Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 789
user691730 Avatar asked Nov 01 '12 20:11

user691730


2 Answers

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, i.e. 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.

like image 97
Slauma Avatar answered Sep 30 '22 14:09

Slauma


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();
like image 43
user3104239 Avatar answered Sep 30 '22 13:09

user3104239