这是我的POCO
public class Game
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Galaxy> Galaxies { get; set; }
}
这是TypeConfiguration ....
public class GameConfiguration : EntityTypeConfiguration<Game>
{
public GameConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
HasMany(x => x.Galaxies);
Property(x => x.Name)
.IsRequired()
.HasMaxLength(50);
}
}
我的问题是这个...为什么,当这个作为迁移添加时,迁移代码没有将“Name”属性设置为“NOT NULL”?它也会忽略MaxLength设置。这是为什么?
CreateTable(
"dbo.Games",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
答案 0 :(得分:1)
乍一看,即使配置构造函数从未运行,配置的其余部分也会按惯例匹配,如果缺少name属性,则可以解释它。缺少在模型构建器中注册配置的代码。
您可以注册实体配置,例如在OnModelCreated方法中,如下所示:
modelBuilder.Configurations.Add(new GameConfiguration());