实体框架4 - 与视图的多对多关系

时间:2013-11-12 09:36:31

标签: c# sql-server entity-framework ef-code-first entity-framework-5

我有一个实体组,它与视图VesselInfo有很多关系。在数据库中,关系存储在表GroupVessel中。

public class Group
{
    public Group()
    {
        GroupVessels = new List<GroupVessel>();
    }

    public int GroupId { get; set; }

    public virtual ICollection<GroupVessel> GroupVessels { get; set; }
}

public class GroupVessel
{
    public int GroupVesselId { get; set; }

    public int GroupId { get; set; }
    public virtual Group Group { get; set; }

    public int VesselId { get; set; }
    public virtual VesselView Vessel { get; set; }
}

public class VesselView
{
    public int VesselId { get; set; }
}

实体在上下文中映射如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new VesselViewMapping());
    modelBuilder.Configurations.Add(new GroupMapping());
    modelBuilder.Configurations.Add(new GroupVesselMapping());
    base.OnModelCreating(modelBuilder);
}

public class VesselViewMapping : EntityTypeConfiguration<VesselView>
{
    public VesselViewMapping()
    {
        // Primary Key
        HasKey(t => t.VesselId);

        // Properties
        Property(t => t.VesselId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        // Table & Column Mappings
        ToTable("v_VesselInfo");
        Property(t => t.VesselId).HasColumnName("VesselId");
    }
}

public class GroupMapping : EntityTypeConfiguration<Group>
{
    public GroupMapping()
    {
        // Primary Key
        HasKey(group => group.GroupId);

        // Properties

        // Table & Column Mappings
        ToTable("Group");
        Property(t => t.GroupId)
            .HasColumnName("GroupId")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class GroupVesselMapping : EntityTypeConfiguration<GroupVessel>
{
    public GroupVesselMapping()
    {
        // Primary Key
        HasKey(group => group.GroupVesselId);

        // Properties

        // Table & Column Mappings
        ToTable("GroupVessel");
        Property(t => t.GroupVesselId)
            .HasColumnName("GroupVesselId")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(t => t.VesselId).HasColumnName("VesselId").IsRequired();

        Property(t => t.GroupId).HasColumnName("GroupId");

        // Relationships
        HasRequired(t => t.Group).WithMany(g => g.GroupVessels)
                                 .HasForeignKey(t => t.GroupVesselId);
    }
}

当我尝试实例化上下文时,我收到以下错误:

异常详细信息:System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或多个验证错误:

\ tSystem.Data.Entity.Edm.EdmAssociationEnd ::多重性在关系'GroupVessel_Group'中的角色'GroupVessel_Group_Source'中无效。由于“从属角色”是指关键属性,因此从属角色的多重性的上限必须为“1”。

其他一些信息: 在数据库中,GroupVessel.VesselId列是表Vessels的外键,它是v_VesselInfo视图的基础表。从VesselView到GroupVessel没有导航属性,因为不需要在应用程序中以这种方式遍历图形。

1 个答案:

答案 0 :(得分:0)

最后想通了,我使用了错误的字段作为外键字段,更改为GroupId并且可以正常工作