我正在尝试使用我自己的对象类型创建一个 Code First 类并收到此错误:
.MTObject'
必须是不可为空的值类型才能将其用作泛型类型或方法“”中的参数“TSystem.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration<TStructuralType>.Property<T>(System.Linq.Expressions.Expression<System.Func<TStructuralType,T>>)
”
有没有办法声明我的类属性来克服这个错误?
代码如下:
// Simple Example
public class MTObject
{
public string Object { get; set; }
public MTObject()
{
}
}
public class Person
{
public decimal Id { get; set; }
//public string Name { get; set; }
public MTObject Name { get; set; }
public Int32 Age { get; set; }
}
public class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration() : base()
{
HasKey(p => p.Id);
Property(p => p.Id).HasColumnName("ID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Name).HasColumnName("NAME").IsOptional();
Property(p => p.Age).HasColumnName("AGE").IsOptional();
ToTable("Person");
}
}
public class PersonDataBase : DbContext
{
public DbSet<Person> Persons { get; set; }
public PersonDataBase(string connectionString) : base(connectionString)
{
Database.CreateIfNotExists();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PersonConfiguration());
base.OnModelCreating(modelBuilder);
}
}
// End Simple EXample