【问题标题】:how to create relationship in code first in asp.net core如何在asp.net核心中首先在代码中创建关系
【发布时间】:2019-12-06 00:24:31
【问题描述】:
Failed executing DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AspNetUsers] ADD CONSTRAINT [FK_AspNetUsers_Citys_City_ID] FOREIGN KEY ([City_ID]) REFERENCES [Citys] ([ID]) ON DELETE CASCADE;

ALTER TABLE 语句与FOREIGN KEY 约束FK_AspNetUsers_Citys_City_ID 冲突。数据库UserDBtest3", table "dbo.Citys", column 'ID'发生冲突。

类城市

public class City
    {
        [Key]
        public int ID { get; set; }

        [MaxLength(150)]
        public string Name { get; set; }

        public int State_ID { get; set; }

        [ForeignKey("State_ID")]
        public virtual State State { get; set; }

        public ICollection<ApplicationUser> ApplicationUsers { get; set; }

        public City()
        {
            ApplicationUsers = new List<ApplicationUser>();
        }
    }

类用户

 public class ApplicationUser : IdentityUser<int>
    {
        [Column(TypeName ="nvarchar(150)")]
        public string FullName { get; set; }

        public bool Active { get; set; }

        public int UserRoleID { get; set; }

        [ForeignKey("UserRoleID")]
        public virtual UserRoleTest UserRoleTest { get; set; }

        public int Person_ID { get; set; }

        [ForeignKey("Person_ID")]
        public virtual Person Person { get; set; }

        public int City_ID { get; set; }

        [ForeignKey("City_ID")]
        public virtual City City { get; set; }

        public int State_ID { get; set; }

        [ForeignKey("State_ID")]
        public virtual State State { get; set; }
    }
}

数据上下文

public class AuthenticationContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
    {
        public AuthenticationContext(DbContextOptions<AuthenticationContext> option):base(option)
        {

        }

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }

        public DbSet<UserRoleTest> UserRoleTests { get; set; }

        public DbSet<Person> Prsons { get; set; }

        public DbSet<Report> Reports { get; set; }

        public DbSet<Message> Messages { get; set; }

        public DbSet<City> Citys { get; set; }

        public DbSet<State> States { get; set; }
    }

【问题讨论】:

    标签: .net-core foreign-keys ef-code-first


    【解决方案1】:

    根据实体框架documentation

    如果您希望按照约定发现关系,可以遵循以下模式:&lt;principal entity name&gt;&lt;primary key property name&gt;

    public class City
    {
        [Key]
        public int CityId { get; set; }
    
        //...omitted for brevity
    }
    
    public class ApplicationUser : IdentityUser<int>
    {
        // omitted for brevity
    
        public City City { get; set; }
        public int CityId { get; set; }
    }
    

    或者如果你不想使用约定,你可以使用[ForeignKey]注解:

    public class City
    {
        [Key]
        public int Id { get; set; }
    
        //...omitted for brevity
    }
    
    public class ApplicationUser : IdentityUser<int>
    {
        // omitted for brevity
    
        [ForeignKey("IdCity")]
        public City City { get; set; }
        public int IdCity { get; set; }
    }
    

    或者,如果您不想拥有外键属性(不推荐)。

    如果没有找到外键属性,则会引入影子外键属性。

    public class City
    {
        [Key]
        public int CityId { get; set; }
    
        //...omitted for brevity
    }
    
    public class ApplicationUser : IdentityUser<int>
    {
        // omitted for brevity
    
        public City City { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      相关资源
      最近更新 更多