实体框架多对多
本文关键字:框架 实体 | 更新日期: 2023-09-27 18:20:40
第一种型号
[Table("Blotter.DealingDesk")]
public partial class DealingDesk
{
public DealingDesk()
{
DealingStatus = new HashSet<DealingStatus>();
}
public int DealingDeskId { get; set; }
[Required]
[StringLength(50)]
public string Country { get; set; }
[Required]
[StringLength(50)]
public string DealingDeskCode { get; set; }
public virtual ICollection<DealingStatus> DealingStatus { get; set; }
}
第二型号
[Table("Blotter.EmailAddress")]
public partial class EmailAddress
{
public EmailAddress()
{
LUDealingDeskEmails = new HashSet<LUDealingDeskEmail>();
}
[Key]
public int EmailId { get; set; }
[Required]
[StringLength(150)]
public string Email { get; set; }
public virtual ICollection<LUDealingDeskEmail> LUDealingDeskEmails { get; set; }
}
第三种型号(查找表为POCO)
[Table("Blotter.LUDealingDeskEmail")]
public partial class LUDealingDeskEmail
{
public int LUDealingDeskEmailId { get; set; }
public int FK_EmailId { get; set; }
public int FK_DealingDeskId { get; set; }
public virtual DealingDesk DealingDesk { get; set; }
public virtual EmailAddress EmailAddress { get; set; }
}
public partial class MCUtilityContext : DbContext
{
public MCUtilityContext()
: base("name=MCUtilityContext")
{
Database.SetInitializer<CapstoneInterfaceContext>(null);
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
}
public virtual DbSet<DealingDesk> DealingDesks { get; set; }
public virtual DbSet<DealingStatus> DealingStatus { get; set; }
public virtual DbSet<EmailAddress> EmailAddresses { get; set; }
public virtual DbSet<LUDealingDeskEmail> LUDealingDeskEmails { get; set; }
public virtual DbSet<ApplicationError> ApplicationErrors { get; set; }
public virtual DbSet<Connection> Connections { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<DealingDesk>()
.HasKey(k => k.DealingDeskId);
modelBuilder.Entity<DealingStatus>()
.HasKey(k => k.DealingStatusId)
.HasRequired(e => e.DealingDesk)
.WithMany( s => s.DealingStatus)
.HasForeignKey( s => s.DealingDeskId)
.WillCascadeOnDelete(false);
//modelBuilder.Entity<DealingDesk>()
// .HasMany(a => a.EmailAddress)
// .WithMany(b => b.)
// .Map(m =>
// {
// m.MapLeftKey("DealingDeskId");
// m.MapRightKey("EmailId");
// m.ToTable("LUDealingDeskEmail", "Blotter");
// });
//modelBuilder.Entity<DealingDesk>()
// .HasMany<EmailAddress>(s => s.EmailAddress)
// .WithMany(c => c.DealingDesk)
// .Map(cs =>
// {
// cs.MapLeftKey("FK_DealingDeskId");
// cs.MapRightKey("FK_EmailId");
// cs.ToTable("Blotter.LUDealingDeskEmail");
// });
}
问题是,当我检索交易台对象时,我没有收到电子邮件,它会返回一个错误:
'(System.Data.Entity.DynamicProxies.DealingDesk_B97FADE5FCA3BA8DED564B236CAB94E3462E9228525392A5055AE860F8156F86)新System.Collections.Generic.Mscorlib_CollectionDebugView(dealingDesk).Items[1]).EmailAddress'引发了类型为的异常"System.Data.Entity.Core.EntityCommandExecutionException"
也许我看错了?
我在尝试访问外键对象时遇到了这个错误,而没有对查询结果序列调用ToList()。以下是示例:
class ForeignKeyObject
{
public int Id{get;set;}
}
class Entity
{
public int Id{get;set;}
public ForeignKeyObj KeyObj{get;set;}
}
以下是重现错误的代码:
var entities = (from e in _db.Entities
where e.Id== 1
select e).ToList();
foreach( var e in entities )
Debug.WriteLine(e.KeyObj.Id.ToString());
如果ToList()调用将被删除,则错误将在Debug.WriteLine()行中显示出来。将其添加回,EF将填充外键对象。