Entity Framework: type "appears as complex type" Error

In my database, I have a table called "tracking" with the following columns:

[OrderNumber] [varchar](50) NULL,
[TrackingNumber] [varchar](50) NULL,
[emailaddress] [varchar](100) NULL,
[courier] [varchar](10) NULL

      

I have a class called Tracking to represent entities from a table:

public class Tracking
{
    public virtual string OrderNumber { get; set; }
    public virtual string TrackingNumber { get; set; }
    public virtual string EmailAddress { get; set; }
    public virtual string Courier { get; set; }
}

      

In the definition of the data context

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new TrackingConfig());
}

internal class TrackingConfig : EntityTypeConfiguration<Tracking>
{
    internal TrackingConfig()
    {
        Property(x => x.OrderNumber);
        Property(x => x.TrackingNumber);
        Property(a => a.EmailAddress).HasColumnName("emailaddress");
        Property(a => a.Courier).HasColumnName("courier");
    }
}

      

As you can see, I am not explicitly casting it as a complex type. I even used the HasColumnName to indicate which column name maps to which property of the class, in case it can't recognize them due to capitalization differences. (The error I'm getting happens regardless of whether the HasColumnName value is used as it turns out).

When I call a repository that I have configured for whatever reason, initialization fails and Entity Framework throws an exception:

The type 'Tracking' is mapped as a complex type. The Set method, DbSet objects, and DbEntityEntry objects can only be used with entity types, not complex types.

      

I am not setting it as a complex type ... why does EF4 treat it as one?

+3


source to share


1 answer


It looks like it has something to do with declaring fields as virtual. You can also use data annotations to override table and column names to match your existing db schema. Following is the way to create POCO model and dbcontext.



[Table("tracking")]
public class Tracking
{
    public string OrderNumber { get; set; }
    public string TrackingNumber { get; set; }

    [Column(Name = "emailaddress")]
    public string EmailAddress { get; set; }

    [Column(Name = "courier")]
    public string Courier { get; set; }
}

public class TrackingEntities : DbContext
{
    public DbSet<Tracking> Trackings { get; set; }
}

      

+1


source







All Articles