0

I'm using an existing database with the Code First approach.

I will be using all new tables in the DB except one existing table that I need to make use of. I only want to be able to read from this table and not have any changes occur if I call update-database. How will I go about this?

If I use the code like it is below then it gets totally excluded from the context but if I remove the ignore line in OnModelCreating then I have issues with the update-database command as it already exists in the db(and Like I mentioned, I do not want to update this table at any time )

public class DBContext : IdentityDbContext<ApplicationUser>
{
    public DBContext()
       : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public DbSet<person> Persons { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Ignore<person>();
    }

    public static DBContext Create()
    {
        return new DBContext();
    }
}
CompiledIO
  • 160
  • 16
  • Does this answer your question? http://stackoverflow.com/questions/10437058/how-to-make-entity-framework-data-context-readonly –  Apr 06 '16 at 12:26
  • Should I have 2 separate context's (thats what I gather looking by that example) – CompiledIO Apr 06 '16 at 12:34
  • I think the crux of the answer is to use [QueryableExtensions.AsNoTracking](https://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions.asnotracking(v=vs.113).aspx) to prevent tracking of the entity by the context. Without tracking, updates will fail. –  Apr 06 '16 at 12:38
  • I tried implementing the example provided but it doesn't work for my situation – CompiledIO Apr 06 '16 at 12:40
  • Then you should [edit] and add details about how you tried to implement this and why it "didn't work". Will help folks answer your question. Good luck (that was the extent of my knowledge on the subject--AsNoTracking is how we do it). –  Apr 06 '16 at 12:41
  • if I implement that example i get the following error, There is already an object named 'person' in the database....... – CompiledIO Apr 06 '16 at 12:42
  • Trust me, click this link -> [edit] and add details (code, and the full error--catch the exception and call `ToString` on it) –  Apr 06 '16 at 12:43

1 Answers1

1

I changed the code to look like the following

public class DBContext : IdentityDbContext<ApplicationUser>
{
public DBContext()
   : base("DefaultConnection", throwIfV1Schema: false)
{
}

//public DbSet<person> Persons { get; set; }
public DbQuery<person> Persons
{
    get
    {
        // Don't track changes to query results
        return Set<person>().AsNoTracking();
    }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<person>();
}

public static DBContext Create()
{
    return new DBContext();
}

}

*Note that my migrations were already enabled at this point

Then I got the error "There is already an object named 'person' in the database."

Then run PM> Add-Migration FirstMigration

After this go and delete the create and drop table in the migration script

run PM> Update-Database and it should work now.

I fixed all of this by using information from this article: https://blog.rajsoftware.com/2014/07/12/entity-framework-code-first-automatic-migration-existing-tables/

CompiledIO
  • 160
  • 16