[英]Entity Framework Fluent API Mapping Issue
如何配置三个表之间的关系以获取所有相关数据?
我有以下模型(和数据库中的相同表):
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public virtual ClientCard ClientCard { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
public virtual ClientCard ClientCard { get; set; }
}
public class ClientCard
{
public Guid ClientCardId { get; set; }
public Guid CardId { get; set; }
public Guid ClientId { get; set; }
public virtual Client Client { get; set; }
public virtual Card Card { get; set; }
}
以及以下OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<ClientCard>()
.HasKey(x => x.ClientCardId);
builder.Entity<ClientCard>()
.HasRequired(x => x.Client)
.WithRequiredPrincipal(x => x.ClientCard);
builder.Entity<ClientCard>()
.HasRequired(x => x.Card)
.WithRequiredPrincipal(x => x.ClientCard);
base.OnModelCreating(builder);
}
但是结果返回时没有相关数据。 为什么?
对于您的代码,请尝试以下操作:
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public virtual ICollection<ClientCard> ClientCard { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
public virtual ICollection<ClientCard> ClientCard { get; set; }
}
public class ClientCard
{
public Guid ClientCardId { get; set; }
public Guid CardId { get; set; }
public Guid ClientId { get; set; }
public virtual Client Client { get; set; }
public virtual Card Card { get; set; }
}
并映射:
builder.Entity<Client>()
.HasKey(t => t.ClinetID);
....
builder.Entity<Client>()
.HasMany(a => a.ClientCard)
.WithRequired(p => p.Client)
.HasForeignKey(p => p.ClientID);
对于卡
....
builder.Entity<Card>()
.HasMany(a => a.ClientCard)
.WithRequired(p => p.Card)
.HasForeignKey(p => p.CardID);
但我认为最好的方法是为每个实体创建一个单独的映射文件
现在,我使用以下模型:
public class Client
{
public Guid ClientId { get; set; }
public string FirstName { get; set; }
public Guid? CardId { get; set; }
public virtual Card Card { get; set; }
}
public class Card
{
public Guid CardId { get; set; }
public string Number { get; set; }
//public virtual Client Client { get; set; }
}
和以下OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<Client>()
.HasOptional(x => x.Card)
//.WithOptionalPrincipal(x=> x.Client)
//or
//.WithOptionalDependent(x=> x.Client)
;
base.OnModelCreating(builder);
}
所有客户均返回相关数据(如果存在)。 但是,如果我想使用Card对象获取相关数据并且取消注释public virtual Client Client { get; set; }
public virtual Client Client { get; set; }
public virtual Client Client { get; set; }
和WithOptionalPrincipal / WithOptionalDependent,我总是了解这些SqlException:“无效的列名'Client_ClientId'”(如果使用了WithOptionalPrincipal)或“无效的列名'Card_CardId'”(如果使用了WithOptionalDependent)
我终于找到了解决方案。
首先,我从Client类中删除了CardId。 其次,我更改了OnModelCreating方法:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
...
builder.Entity<Client>()
.HasOptional(x => x.Card)
.WithOptionalDependent(x => x.Client)
.Map(x => x.MapKey("CardId"))
.WillCascadeOnDelete(false);
builder.Entity<Card>()
.HasOptional(x => x.Client)
.WithOptionalPrincipal(x => x.Card)
.WillCascadeOnDelete(false);
base.OnModelCreating(builder);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.