简体   繁体   中英

must be non-nullable in order to use as parameter 'T'

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

.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>>) '

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

Code is 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

In order to get this line compiling...

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 the property as optional and need to use it as a required property.

Edit

My answer above is wrong. That's not the reason for the compiler error. But the Age property must still be nullable if it is supposed to be optional , ie allow null values.

Edit 2

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

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

(assuming that you actually want to specify the column name for the Object property) Using is IsOptional() is not necessary because string properties are optional by default.

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();

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

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 keyword struct instead of class )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM