jwrightmail
jwrightmail

Reputation: 927

Why is EF code first throwing model backing context exception? Using 4.0.3

Heres the exception:

The model backing the 'ScannerContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

I get this everytime I run my application. I cant figure out what it means. I think it means something isn't mapped correctly, but I cant figure out what. I am using the code first model, and I have an existing database that I want totally custom mappings for. Right now, I have everything in my classes named the same as my database to eliminate possible cuases.

The Exception is thrown when I try to .Add() the entity to the context.

The Entity as it is in the Database

DBimage

The Entity in my DataLayer

public class EAsset
{
    public int i_GID { get; set; }
    public EAssetType Type { get; set; }
    public EOrgEnvironment Environment { get; set; }
    public EUser Contact { get; set; }
    public string s_Name { get; set; }
    public string s_Role { get; set; }
    public DateTime d_Added { get; set; }
    public DateTime d_LastUpdated { get; set; }
    public bool b_Retired { get; set; }

    public EAsset()
    {
        Type = new EAssetType();
        Environment = new EOrgEnvironment();
        Contact = new EUser();
        d_Added = DateTime.Now;
        d_LastUpdated = DateTime.Now;
    }
}

The Context Object (with attempted table mapping and key assignment)

public class ScannerContext : DbContext
{
    public ScannerContext()
        : base("LabDatabase") { }

    public DbSet<EAsset> EAssets { get; set; }
    public DbSet<EAssetType> EAssetTypes { get; set; }
    public DbSet<EOrgEnvironment> EOrgEnvironments { get; set; }
    public DbSet<EUser> EUsers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EAsset>().HasKey(k=>k.i_GID).ToTable("t_Assets");
        modelBuilder.Entity<EAssetType>().HasKey(k => k.i_ID).ToTable("t_Asset_Types");
        modelBuilder.Entity<EOrgEnvironment>().HasKey(k => k.i_ID).ToTable("t_Org_Environments");
        modelBuilder.Entity<EUser>().HasKey(k => k.i_ID).ToTable("t_Users");

        base.OnModelCreating(modelBuilder);

    }
}

The Program

class Program
{
    static void Main(string[] args)
    {
        EAsset Entity = new EAsset { s_Name = "jewri-pc" };
        var sContext = new ScannerContext();
        sContext.EAssets.Add(Entity);
        sContext.SaveChanges();
    }
}

Upvotes: 3

Views: 7560

Answers (2)

user4302561
user4302561

Reputation: 1

Enable-Migrations -ContextTypeName EmployeeProject.Models.DepartmentContext

Means you have to write your project name.Models.Context name

It will work.

Upvotes: 0

jwrightmail
jwrightmail

Reputation: 927

For EF runtime version 4.0.3 / version 4.0

public class ScannerContext : DbContext
{
    public ScannerContext()
        : base("LabDatabase") { }

    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        Database.SetInitializer<ScannerContext>(null); // <--- This is what i needed

        ...

        base.OnModelCreating(modelBuilder);

    }
}

With that code installed I am now chasing errors related to having all my relationships accounted for in the model. The FK Constraints are forcing me to add the missing relational items.

Found info here. They explain the importance a bit.

The model backing the <Database> context has changed since the database was created

Upvotes: 4

Related Questions