定义多对多关系在代码中第一个实体框架 [英] Defining many to many relation in code first entity framework

查看:247
本文介绍了定义多对多关系在代码中第一个实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个班在我的模型,你可以看到下面。

I have 3 classes in my model as you can see below.

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string UserName { get; set; }

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}

[Table("MartialArt")]
public class MartialArt
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string IconPath { get; set; }

    public string ImagePath { get; set; }

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}

public class MartialArtUserProfile
{
    public int UserProfileId { get; set; }
    public UserProfile UserProfile { get; set; }

    public int MartialArtId { get; set; }
    public MartialArt MartialArt { get; set; }
}

和我有一个配置类为许多人如下一对多的关系:

And I have a configuration class for many to many relationship as below:

public class MartialArtUserProfileConfiguration : EntityTypeConfiguration<MartialArtUserProfile>
{
    public MartialArtUserProfileConfiguration()
    {
        HasKey(a => new { a.MartialArtId, a.UserProfileId });

        HasRequired(a => a.MartialArt)
            .WithMany(s => s.MartialArtUserProfiles)
            .HasForeignKey(a => a.MartialArtId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.UserProfile)
            .WithMany(p => p.MartialArtUserProfiles)
            .HasForeignKey(a => a.UserProfileId)
            .WillCascadeOnDelete(false);
    }
}



定义我的实体的关系,当我尝试运行后更新 - 数据库软件包管理器控制台,它说:

一个或多个模型生成过程中检测到验证错误:

One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType:的EntityType'MartialArtUserProfile没有定义键。定义此的EntityType的关键。
\tSystem.Data.Entity.Edm.EdmEntitySet:的EntityType:EntitySet的'MartialArtUserProfiles'是基于类型'MartialArtUserProfile'是没有定义的键

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'MartialArtUserProfile' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'MartialArtUserProfiles' is based on type 'MartialArtUserProfile' that has no keys defined.

我是什么事先做错了吗?

What am I doing wrong?

谢谢,

推荐答案

如果我明白你只是想创建一个多对多与传递表。如果是这样,这是另一种方式来处理这个。用流利的API,如下图。您可以更改 UserProfileToMartialArt 来任何你想要的表名是。而不是创建 MartialArtUserProfile 模式让EF为您创建的中间地带。这还指定你的钥匙应该让你周围的误差

If I understand you are simply trying to create a many to many with a transitive table. If so this is another way to approach this. Use Fluent API to map as below. You can change the UserProfileToMartialArt to whatever you want the table name to be. Instead of creating the MartialArtUserProfile model let EF create the middle ground for you. This also specifies your keys which should get you around the error.

modelBuilder.Entity<UserProfile>()
    .HasMany(b => b.MartialArts)
    .WithMany(a => a.UserProfiles)
    .Map(m => m.MapLeftKey("MartialArtId")
        .MapRightKey("UserProfileId")
        .ToTable("UserProfileToMartialArt"));

在武术型号放在

public IList<UserProfile> UserProfiles { get; set; }   

在用户配置型号放在

public IList<MartialArt> MartialArts { get; set; }  

这篇关于定义多对多关系在代码中第一个实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆