使用 Fluent 配置关系时出错
Error configuring relationship using Fluent
我在尝试为现有 table 配置我的实体和关系时遇到以下错误:
Models.Foo_Bar: : Multiplicity conflicts with
the referential constraint in Role 'Foo_Bar_Target'
in relationship 'Foo_Bar'. Because all of the
properties in the Dependent Role are non-nullable, multiplicity of the
Principal Role must be '1'.
我尝试建模的系统是,如果用户拥有 Foo
,它会赋予零个或多个 Bar
,相反,用户可能拥有 Bar
而没有相应的 Foo
Table配置如下:
(不确定是否相关,但实际上有几种不同类型的 Foo
可以映射到 Bar
并且 Foo
使用 Table 数据库中的每个层次结构)
用户:
Id INT NOT NULL IDENTITY(1, 1)
UserFoo:
Id INT NOT NULL IDENTITY (1, 1)
UserId INT NOT NULL
//Below is to do with TPH implementation, hopefully not
//relevant here but included for sake of completeness
FooType1Id INT NULL
FooType2Id INT NULL
用户栏:
UserId INT NOT NULL
UserFooId INT NULL
BarId INT NOT NULL
和实体 类 映射如下:
public abstract class UserFoo
{
public int Id { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class UserBar
{
public int UserId { get; set; }
public int? UserFooId { get; set; }
public int BarId { get; set; }
public virtual User User { get; set; }
public virtual UserFoo UserFoo { get; set; }
public virtual Bar Bar { get; set; }
}
映射是这样完成的:
public class UserFooMap : EntityTypeConfiguration<UserFoo>
{
public UserFooMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.UserId).IsRequired();
// Table & Column Mappings
this.ToTable("UserFoo", "User");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.UserId).HasColumnName("UserId");
// Relationships
this.HasMany(t => t.UserBar)
.WithOptional(t => t.UserFoo)
.HasForeignKey(t => t.UserFooId);
}
}
public class UserBarMap : EntityTypeConfiguration<UserBar>
{
public UserBarMap()
{
// Primary Key
this.HasKey(t => new { t.UserId, t.BarId, t.UserFooId });
// Table & Column Mappings
this.ToTable("UserBar", "User");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.UserFooId).HasColumnName("UserFooId");
this.Property(t => t.BarId).HasColumnName("BarId");
// Relationships
this.HasOptional(t => t.UserFoo)
.WithMany(t => t.UserBar)
.HasForeignKey(t => t.UserFooId);
}
}
从错误判断,我认为问题出在 UserFooId
在 UserBar
上可以为空,但我在这里看不到错误。我对这两个代码都比较陌生(我知道这在技术上不是因为我们试图映射到现有的数据库)和 Table Per Hierarchy 以及
问题出在 UserBar
Table 上,数据库中的列 UserFooId
未设为可为空。将此列正确设置为可为空后,一切正常。
我在尝试为现有 table 配置我的实体和关系时遇到以下错误:
Models.Foo_Bar: : Multiplicity conflicts with the referential constraint in Role 'Foo_Bar_Target' in relationship 'Foo_Bar'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
我尝试建模的系统是,如果用户拥有 Foo
,它会赋予零个或多个 Bar
,相反,用户可能拥有 Bar
而没有相应的 Foo
Table配置如下:
(不确定是否相关,但实际上有几种不同类型的 Foo
可以映射到 Bar
并且 Foo
使用 Table 数据库中的每个层次结构)
用户:
Id INT NOT NULL IDENTITY(1, 1)
UserFoo:
Id INT NOT NULL IDENTITY (1, 1)
UserId INT NOT NULL
//Below is to do with TPH implementation, hopefully not
//relevant here but included for sake of completeness
FooType1Id INT NULL
FooType2Id INT NULL
用户栏:
UserId INT NOT NULL
UserFooId INT NULL
BarId INT NOT NULL
和实体 类 映射如下:
public abstract class UserFoo
{
public int Id { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class UserBar
{
public int UserId { get; set; }
public int? UserFooId { get; set; }
public int BarId { get; set; }
public virtual User User { get; set; }
public virtual UserFoo UserFoo { get; set; }
public virtual Bar Bar { get; set; }
}
映射是这样完成的:
public class UserFooMap : EntityTypeConfiguration<UserFoo>
{
public UserFooMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.UserId).IsRequired();
// Table & Column Mappings
this.ToTable("UserFoo", "User");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.UserId).HasColumnName("UserId");
// Relationships
this.HasMany(t => t.UserBar)
.WithOptional(t => t.UserFoo)
.HasForeignKey(t => t.UserFooId);
}
}
public class UserBarMap : EntityTypeConfiguration<UserBar>
{
public UserBarMap()
{
// Primary Key
this.HasKey(t => new { t.UserId, t.BarId, t.UserFooId });
// Table & Column Mappings
this.ToTable("UserBar", "User");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.UserFooId).HasColumnName("UserFooId");
this.Property(t => t.BarId).HasColumnName("BarId");
// Relationships
this.HasOptional(t => t.UserFoo)
.WithMany(t => t.UserBar)
.HasForeignKey(t => t.UserFooId);
}
}
从错误判断,我认为问题出在 UserFooId
在 UserBar
上可以为空,但我在这里看不到错误。我对这两个代码都比较陌生(我知道这在技术上不是因为我们试图映射到现有的数据库)和 Table Per Hierarchy 以及
问题出在 UserBar
Table 上,数据库中的列 UserFooId
未设为可为空。将此列正确设置为可为空后,一切正常。