由于没有系统地学习过EF。就在项目中使用,造成许多莫名的错误。尤其是在复杂对象的存储上,更是伤透脑筋。今天对复杂对象存储作了一个总结。

 

订单Order->订单项OrderItem->商品Good->零件Part

 

其中订单和订单项一对多,商品和订单项一对多,商品和零件多对多。

 

 

代码:

public class Order

{

[Key()]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ID { get; set; }

 

public string Name { get; set; }

 

public virtual List<OrderItem> OrderItems { get; set; }

 

public Order()

{

OrderItems = new List<OrderItem>();

}

}

public class OrderItem

{

[Key()]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ID { get; set; }

 

public string Name { get; set; }

 

public int GoodID { get; set; }

public virtual Good Good { get; set; }

 

public int OrderID { get; set; }

public virtual Order Order { get; set; }

}

public class Good

{

[Key()]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ID { get; set; }

 

public string Name { get; set; }

 

public virtual List<OrderItem> OrderItems { get; set; }

public virtual List<Part> Parts { get; set; }

 

public Good()

{

OrderItems = new List<OrderItem>();

Parts = new List<Part>();

}

}

public class Part

{

[Key()]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ID { get; set; }

public string Name { get; set; }

public virtual List<Good> Goods { get; set; }

public Part()

{

Goods = new List<Good>();

}

}

public class CDbContext : DbContext

{

private readonly static string CONNECTION_STRING = "name=Test_ConnString";

public CDbContext()

: base(CONNECTION_STRING)

{

 

}

 

public DbSet<Order> OrderSet { get; set; }

public DbSet<OrderItem> OrderItemSet { get; set; }

public DbSet<Good> GoodSet { get; set; }

public DbSet<Part> PartSet { get; set; }

 

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.Entity<Order>().HasMany<OrderItem>(i => i.OrderItems)

.WithRequired(i => i.Order).HasForeignKey(i => i.OrderID);

 

modelBuilder.Entity<OrderItem>().HasRequired<Good>(i => i.Good)

.WithMany(i => i.OrderItems)

.HasForeignKey(i => i.GoodID);

 

modelBuilder.Entity<Good>().HasMany<Part>(i => i.Parts)

.WithMany(i => i.Goods).Map(i => i.MapLeftKey("GoodID")

.MapRightKey("PartID").ToTable("GoodPart"));

}

}

public class CDbInit : DropCreateDatabaseIfModelChanges<CDbContext>

{

protected override void Seed(CDbContext context)

{

//var part = new Part();

//part.Name = "Part1";

//context.PartSet.Add(part);

//context.SaveChanges();

}

}

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<connectionStrings>

<add name="Test_ConnString"

connectionString="Persist Security Info = true;Data Source=SINPC\SIN2000;Initial Catalog=TestDb;User Id=sa;Password=sa;"

providerName="System.Data.SqlClient"/>

</connectionStrings>

</configuration>

private void button1_Click(object sender, EventArgs e)

{

Part part = new Part();

Good good = new Good();

 

using (CDbContext context = new CDbContext())

{

part.Name = "part1";

 

good.Name = "rice1";

good.Parts.Add(part);

 

Order order = new Order();

order.Name = "order1";

 

OrderItem item = new OrderItem();

 

item.Good = good;

item.Order = order;

 

item.Name = "item1";

 

order.OrderItems.Add(item);

 

context.OrderSet.Add(order);

context.SaveChanges();

}

}

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Database.SetInitializer<CDbContext>(new CDbInit());

Application.Run(new Form1());

}

}

 

 

代码贴完了。调试心得,要保存一个复杂对象,不需要一个简单对象一个简单对象的保存,EF会自动帮我们把简单对象同时保存。

只需要理清这个复杂对象的一条线,通往各简单对象。就可以实现复杂对象的保存,另外

EF还会自动识别哪些简单对象需要保存,哪些不需要保存。

 

源码地址:http://download.csdn.net/download/sinxuesong1/4111163