【发布时间】:2013-12-20 18:52:16
【问题描述】:
我正在尝试使用 Code First 创建 SQL CE 4 数据库。运行下面的示例代码时,Entity Framework 每次都会为 product 插入新记录,即使数据完全相同。 我需要做什么才能使 Entity Framework 不创建重复的关联产品? ForeignID1 和 Product 对象中的值是数据库中已经存在的值,但是 Entity Framework 正在擦除我提供的 ID它并添加一个新 ID。
namespace MyApp.Model
{
public class MyThing
{
public int ID { get; set; }
[ForeignKey("Product")]
public int ForeignID1{ get; set; }
public virtual Product Product { get; set; }
}
}
// Data.DataManager.cs
public class DataManager : DbContext
{
public DbSet<Model.MyThing> Things{ get; set; }
public DbSet<Model.Product> Products { get; set; }
}
这些是它输入的值。表中应该只有一个值被多个MyThings's 引用
【问题讨论】:
标签: c# entity-framework