我想创建我自己的对象类型守则第一类,并得到这个错误:必须是非空的,以便作为参数使用“T”
.MTObject'
must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.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
这是.net吗?无论如何,请用语言标签标记问题(也许删除一个不太相关的标签,如“编译器错误”) – Bohemian