代码优先实体框架可能与其他不起作用的字段有很多关系

时间:2017-01-31 06:14:30

标签: c# entity-framework migration

我有3个必须处于多对多关系的类,但是当我运行我的项目时,Entity Framework无法将它们映射到SQL Server表。我使用了迁移。

我的用户类在这里:

 [Table("tbl_User")]
    public class User
    {
                public User()
                {
                }

                #region Properties
                [Key]
                [Required][DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]               
                public long Id { get; set; }                
                public string FirstName { get; set; }               
                public string LastName { get; set; }
                public string LoginName { get; set; }
                public string Email { get; set; }
                public string Password { get; set; }
                public bool EmailVerify { get; set; }
                public string Image { get; set; }               
                public DateTime CreateDate { get; set; }
                public DateTime UpdateDate { get; set; }
                public DateTime LoginDate { get; set; }
                public bool Enabled { get; set; }
                public bool Deleted { get; set; }
                #endregion

                #region Relations               
                public virtual IList<UserRole> UserRole { get; set; }          
                #endregion Relations 
    }

角色类在这里:

[Table("tbl_Role")]
public class Role
{
        public Role()
        {
        }

        #region Properties
        [Key]
        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]      
        public int Id { get; set; }
        public string RoleName { get; set; }
        public int Order { get; set; }
        public string Icon { get; set; }                
        public DateTime CreateDate { get; set; }
        public DateTime UpdateDate { get; set; }           
        public bool Enable { get; set; }    
        public bool Deleted { get; set; }
        #endregion Properties

        #region Relations                       
        public virtual IList<UserRole> UserRole { get;set;}


        #endregion Relations
}

UserRole是我的多对多关系表:

[Table("tbl_UserRole")]
public class UserRole
{
            internal class Configuration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<UserRole>
            {
                public Configuration()
                {
                    HasRequired(current => current.User)
                         .WithMany(userrole => userrole.UserRole)
                         .HasForeignKey(fk => fk.UserId);

                    HasRequired(current => current.Role)
                         .WithMany(userrole => userrole.UserRole)
                         .HasForeignKey(fk => fk.RoleId);
                }
            }

            public UserRole()
            {
            }

            #region Properties
            [Key]
            [Required]               
            public long Id { get; set; }        
            public DateTime GrantDate { get; set; }
            public DateTime ExpireDate { get; set; }
            public bool Enabled { get; set; }
            public bool Deleted { get; set; }
            #endregion Properties

            #region Relations
            public long UserId { get; set; }
            public virtual User User { get; set; }
            public int RoleId { get; set; }
            public virtual Role Role { get; set; }
            #endregion Relations
}

这是我的数据库上下文

 public class LiveMiracleDbContext:DbContext
    {
            public LiveMiracleDbContext() : base("LMConnection")
            {
            }

            public DbSet<User> tbl_User { get; set; }
            public DbSet<Role> tbl_Role { get; set; }
            public DbSet<UserRole> tbl_UserRole { get; set; }
    }

当我运行没有这些类的项目时,会生成我的数据库,但是当我使用这些类时,我的数据库不会生成。

1 个答案:

答案 0 :(得分:0)

对于Gert Arnold评论的好坦克

问题在我的属性中这个结构是真的

我删除了属性。