Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Fluent配置关系时出错_C#_Ef Code First_Entity Framework 6 - Fatal编程技术网

C# 使用Fluent配置关系时出错

C# 使用Fluent配置关系时出错,c#,ef-code-first,entity-framework-6,C#,Ef Code First,Entity Framework 6,尝试为现有表配置实体和关系时,我遇到以下错误: Models.Foo_Bar::多重性与 角色“Foo\u Bar\u Target”中的引用约束 在关系“Foo_Bar”中。因为所有的 依赖角色中的属性不可为Null,为 主体角色必须为“1” 我试图建模的系统是,如果用户有一个Foo,它会授予零个或多个Bar,相反,用户可能有一个Bar,而没有相应的Foo 这些表的配置如下所示: (不确定它是否相关,但实际上有几种不同类型的Foo可以映射到条,并且Foo在数据库中使用每个层次结构的表表示) 用

尝试为现有表配置实体和关系时,我遇到以下错误:

Models.Foo_Bar::多重性与 角色“Foo\u Bar\u Target”中的引用约束 在关系“Foo_Bar”中。因为所有的 依赖角色中的属性不可为Null,为 主体角色必须为“1”

我试图建模的系统是,如果用户有一个
Foo
,它会授予零个或多个
Bar
,相反,用户可能有一个
Bar
,而没有相应的
Foo

这些表的配置如下所示:

(不确定它是否相关,但实际上有几种不同类型的
Foo
可以映射到
,并且
Foo
在数据库中使用每个层次结构的表表示)

用户:

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; }
}
公共抽象类UserFoo
{
公共int Id{get;set;}
public int UserId{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟ICollection条{get;set;}
}
公共类用户栏
{
public int UserId{get;set;}
public int?UserFooId{get;set;}
公共int BarId{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟UserFoo UserFoo{get;set;}
公共虚拟条{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);
    }
}
public类UserFooMap:EntityTypeConfiguration
{
公共UserFooMap()
{
//主键
this.HasKey(t=>t.Id);
//性质
this.Property(t=>t.UserId).IsRequired();
//表和列映射
这个.ToTable(“UserFoo”,“User”);
this.Property(t=>t.Id).HasColumnName(“Id”);
this.Property(t=>t.UserId).HasColumnName(“UserId”);
//关系
this.HasMany(t=>t.UserBar)
.WithOptional(t=>t.UserFoo)
.HasForeignKey(t=>t.UserFooId);
}
}
公共类UserBarMap:EntityTypeConfiguration
{
公共UserBarMap()
{
//主键
this.HasKey(t=>new{t.UserId,t.BarId,t.UserFooId});
//表和列映射
这个.ToTable(“用户栏”、“用户”);
this.Property(t=>t.UserId).HasColumnName(“UserId”);
this.Property(t=>t.UserFooId).HasColumnName(“UserFooId”);
this.Property(t=>t.BarId).HasColumnName(“BarId”);
//关系
this.HasOptional(t=>t.UserFoo)
.WithMany(t=>t.UserBar)
.HasForeignKey(t=>t.UserFooId);
}
}

从错误判断,我认为问题源于
UserFooId
UserBar
上可为空,但我在这里看不到错误。我对代码优先(我知道这在技术上不是因为我们试图映射到一个现有的数据库)和每个层次结构的表都是比较陌生的

问题出在
UserBar
表上,
UserFooId
列在数据库中不可为空。一旦此列被正确设置为可空,所有内容都正常工作

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);
    }
}