C# 如何建立MySQL表之间的关系

C# 如何建立MySQL表之间的关系,c#,asp.net-mvc,C#,Asp.net Mvc,我想在MySQL数据库中的3个表之间建立关系。用户可以有多个品牌->一个品牌可以有多个Carmodel。一个CarModel不能有更多的品牌。品牌和汽车模型必须是独一无二的。只有用户登录后,才能添加品牌和Carmodel。我使用ASP.NET核心用户标识 这是我的品牌模式: public class Brand { [Key] public int Id { get; set; } [Required] [StringLength(50)] public

我想在MySQL数据库中的3个表之间建立关系。用户可以有多个品牌->一个品牌可以有多个Carmodel。一个CarModel不能有更多的品牌。品牌和汽车模型必须是独一无二的。只有用户登录后,才能添加品牌和Carmodel。我使用ASP.NET核心用户标识

这是我的品牌模式:

public class Brand
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string BrandName { get; set; }

    [ForeignKey("Author")]
    public string AuthorId { get; set; }

    public virtual ApplicationUser Author { get; set; }
}
这是我的卡莫德尔

 public class CarModel
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Model { get; set; }

    [ForeignKey("Brand")]
    public string BrandId { get; set; }
}
这是我的应用程序上下文

 public virtual DbSet<Brand> Brands { get; set; }

    public virtual DbSet<CarModel> CarModels { get; set; }
公共虚拟数据库集{get;set;}
公共虚拟DbSet CarModels{get;set;}

有人能告诉我如何建立表与表之间的关系吗?提前谢谢

首先,创建集合实体,即品牌。然后引用该应用程序中ApplicationUser实体的Id

相互声明具有导航属性的两个类。用主键上的
ForeignKey
属性标记其中一个表(从属表)。EF由此推断出一对多:

public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }

        public ApplicationUser()
        {
           Brands = new Collection<Brand>();
        }

        public ICollection<Brand> Brands { get; set; }
    }

public class Brand
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string BrandName { get; set; }

    [ForeignKey("Author")]
    public string AuthorId { get; set; }

    public virtual ApplicationUser Author { get; set; }

    public virtual List<CarModel> CarsModel { get; set; }
}

 public class CarModel
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Model { get; set; }

    [ForeignKey("Brand")]
    public string BrandId { get; set; }
}
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(ApplicationUserManager)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
返回用户身份;
}
公共应用程序用户()
{
品牌=新系列();
}
公共ICollection品牌{get;set;}
}
大众品牌
{
[关键]
公共int Id{get;set;}
[必需]
[长度(50)]
公共字符串BrandName{get;set;}
[外侨(“作者”)]
公共字符串AuthorId{get;set;}
公共虚拟应用程序用户作者{get;set;}
公共虚拟列表CarsModel{get;set;}
}
公共级CarModel
{
[关键]
公共int Id{get;set;}
[必需]
[长度(50)]
公共字符串模型{get;set;}
[外键(“品牌”)]
公共字符串BrandId{get;set;}
}
}