问题描述
我今天早上从Entity Framework收到此错误:
I received this error from Entity Framework this morning:
发生了一种关系多重约束,即:EntityReference不能有多个相关对象,但是该查询返回了多个相关对象。这是不可恢复的错误。
该错误是指 User之间的非常简单的一对多关系
和地址
public class User
{
public Guid Id { get; set; }
public virtual IList<Address> Addresses { get; set; }
}
public class Address
{
public Guid Id { get; set; }
public User User { get; set; }
}
我正在使用Fluent API配置实体之间的关系,但是在在这种情况下,这种关系似乎很简单,首先我没有指定任何特定规则,而是让EF推论该关系:
I'm using Fluent API to configure the relationship between entities, but in this case the relationship seems to simple that at first I didn't specify any particular rule and I let EF "deduce" the relationship:
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
HasKey(a => a.Id);
Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
}
}
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(u => u.Id);
Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
}
}
但是收到错误后,我尝试指定以下内容AddressConfiguration中的规则:
But after receiving the error I tried to specify the following rule in the AddressConfiguration:
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
HasKey(a => a.Id);
Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
HasOptional(x => x.User).WithMany(); // I tried adding this
}
}
尝试生成一个新的自动迁移,这是我获得的:
After doing that, I tried generating a new automatic migration, and this is what I obtained:
public partial class AddressFixMigration : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.Addresses", "User_Id", "dbo.Users");
AddColumn("dbo.Addresses", "User_Id1", c => c.Guid());
CreateIndex("dbo.Addresses", "User_Id1");
AddForeignKey("dbo.Addresses", "User_Id1", "dbo.Users", "Id");
}
public override void Down()
{
DropForeignKey("dbo.Addresses", "User_Id1", "dbo.Users");
DropIndex("dbo.Addresses", new[] { "User_Id1" });
DropColumn("dbo.Addresses", "User_Id1");
AddForeignKey("dbo.Addresses", "User_Id", "dbo.Users", "Id");
}
}
我发现这很奇怪。在地址表具有外键 User_Id的情况下,Db似乎还可以,但是在配置文件中指定了一对多关系后,EF想要创建一个不同外键。为什么?
I found this very odd. The Db seemed ok even before, with the Addresses Table having a Foreign Key "User_Id", but after specifying the one-to-many relationship in the Configuration file EF wants to create a different Foreign Key. Why??
我还尝试指定 HasOptional(x => x.User).WithMany()。Map(x => x.MapKey( User_Id));
,但是在那种情况下,当我尝试创建自动迁移时,会收到以下错误:
I also tried to specify HasOptional(x => x.User).WithMany().Map(x => x.MapKey("User_Id"));
, but in that case when I try to create the automatic migration I receive the following error:
User_Id:名称:类型中的每个属性名称都必须是唯一的。属性名称'User_Id'已经定义。
我的数据库似乎显然有问题,但是我看不到什么以及为什么。
It seems like something is clearly wrong with my DB, but I can't see what and why.
推荐答案
我意识到我需要更改配置文件,如下所示:
I realized that I needed to change my Configuration file as it follows:
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
HasKey(a => a.Id);
Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
HasOptional(x => x.User).WithMany(x => x.Addresses).Map(x => x.MapKey("User_Id"));
}
}
.WithMany()
(无参数)用于关系的另一端没有导航属性时,而在这种情况下,我需要将 .WithMany(x => x.Addresses )
,因为用户
实际上包含地址列表。
.WithMany()
(without parameter) is for when there is no navigation property on the other side of the relationship, while in this case I needed to spacify .WithMany(x => x.Addresses)
because User
actually contains a list of Addresses.
这篇关于实体框架“发生关系多重约束违反”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!