Must be non-empty to use 'T' as a parameter

I am trying to create a Code First class with my own object types and get this error:

.MTObject' must be a type with a null value in order to use it as the "T" parameter in the general type or method of System.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration<TStructuralType>.Property<T>(System.Linq.Expressions.Expression<System.Func<TStructuralType,T>>)

Is there a way to declare my class property to pass this error?

Code below:

 // 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 
+6
source share
3 answers

To get this string compilation ...

 Property(p => p.Age).HasColumnName("AGE").IsOptional(); 

... you need to make the Age property nullable:

 public Nullable<Int32> Age { get; set; } 

(or public int? Age { get; set; } )

Or you cannot specify a property as optional and use it as a necessary property.

Edit

My answer above is wrong. This is not the cause of the compiler error. But the Age property should still be NULL if it should be optional, i.e. Allow null values.

Edit 2

In your model, MTObject is a complex type (not an entity) because, by convention, EF cannot derive a primary key property. For a complex type, you can map nested properties as:

 Property(p => p.Name.Object).HasColumnName("NAME"); 

(assuming you really want to specify the column name for the Object property). Using IsOptional() not required since string properties are optional by default.

+3
source

just to help other people

in this case just change

 Property(p => p.Name).HasColumnName("NAME").IsOptional(); 

to

 Property(p => p.Name.Object).HasColumnName("NAME").IsOptional(); 
+3
source

"non-nullable" is not an important part of this error message. The important bits are "there must be a value type". ("non-nullable" is just a value type modifier)

Try

 public struct MTObject { public string Object { get; set; } } public struct Person { public decimal Id { get; set; } //public string Name { get; set; } public MTObject Name { get; set; } public Int32 Age { get; set; } } 

(note the struct keyword instead of class )

0
source

All Articles