[英]Trouble with foreign key and inserting record with EF
我有以下课程:
public class Instrument
{
public int Id { get; set; }
public string Book { get; set; }
public string Page { get; set; }
}
public class Order
{
public int Id { get; set; }
public string OrderName { get; set; }
public ICollection<MatchedInstrument> MatchedInstruments { get; set; }
}
public class MatchedInstrument
{
public int Id { get; set; }
//public int InstrumentId { get; set; }
public Instrument Instrument { get; set; }
public bool IsIncluded { get; set; }
public string Notes { get; set; }
}
以及以下EF DbContext:
public class AppContext : DbContext
{
public DbSet<Instrument> Instruments { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().HasKey(o => o.Id)
.HasMany(o => o.MatchedInstruments)
.WithMany().Map(m =>
{
m.MapLeftKey("orderid");
m.MapRightKey("selectedmatchid");
m.ToTable("ordermatchedinstruments");
});
modelBuilder.Entity<MatchedInstrument>().HasKey(m => m.Id)
.HasRequired(m => m.Instrument)
.WithRequiredPrincipal();
}
}
请注意, OrderMatchedInstruments表是一个联接表,仅包含两列:orderid和matchedinstrumentid(与MatchedInstruments表相关)。 MatchedInstruments表的模式如下所示:
[dbo].[MatchedInstruments](
[Id] [int] IDENTITY(1,1) NOT NULL,
[IsIncluded] [bit] NOT NULL,
[Notes] [varchar](max) NULL,
[InstrumentId] [int] NOT NULL
这一切似乎都可以很好地用于查询数据。 我能够查询订单并包括其匹配的工具。
但是, 当我尝试向订单中添加新的MatchInstrument时,出现一条错误消息,告诉我InstrumentId在MatchInstrument表中不能为null。
...
// Demo Code - count on non-null objects
var instrument = _context.Instruments.FirstOrDefault();
var order = _context.Orders.SingleOrDefault(o => o.Id == 5);
order.MatchedInstruments.Add(new MatchedInstrument() { Instrument = instrument });
_context.SaveChanges();
这使我认为我需要向MatchedInstrument类添加InstrumentId属性。 但是,我认为只要遵循适当的命名约定,EF就可以在没有外键的情况下正常工作。 IOW,我具有Instrument的导航属性这一事实使我认为它会自动在表中查找InstrumentId,因此会为我填充它。
是不是这样,或者我在EF如何处理外键方面缺少某些东西?
将以下属性添加到模型中的每个ID属性:
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
我刚刚得到这个工作。 我在映射中建立的关系不正确。 我从使用WithRequiredPrincipal更改为以下内容:
modelBuilder.Entity<MatchedInstrument>().HasKey(m => m.Id)
.HasRequired(m => m.Instrument)
.WithMany().HasForeignKey(m => m.InstrumentId);
不知道为什么我认为我需要使用WithRequiredPrincipal(),但这肯定解决了我的问题。
如我所见,您发送的不是InstrumentId,而是对象Instrument,这是造成问题的原因,因为您的架构将InstrumentId作为Not Null整数,并且您发送的是整个Instrument对象,因此您只需要确保您真正需要什么数据,然后更改代码。
Grettings ... Ing。 豪尔赫·佩雷斯·巴尔恰纳斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.