jojo
jojo

Reputation: 13843

Table Mapping problem

I am using entity framework 4 in my current project, to read data from several table. Compare using ADO.net, it EF is very easy, with simple code that can do lots of work or me.

But there is one problem...

e,g there is and exiting table call Table "MTable" i only want to query two column from this table, however this table is share with other two ppl who are also working on it. they might add column or modify constrains on this table. Only one thing i am sure is that, the two column i want to query they wont delete it or rename it.

My application is runing now, but from time to time it break because the i generate code from database schema, every time some one do some update to "MTable", i need to update the mapping of my application.

Is there a way to do "Code-fist" maping, allow me to write a simple mode to map to "MTable", and only map the two column, so that i can regardless what what other ppl do on "MTable"???

Thanks

Upvotes: 1

Views: 1244

Answers (4)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Your question is very unclear. You are saying that you are generating the code from your schema and in the same time you are asking if there is a way to map it with code first.

DbContext API != the code first approach. People should differ between Fluent / Annotations mapping and the code-first approach where the database is generated from the code. If you generate the code from the database you are obviously using the database first approach.

Solution for the database-first: Database view but it will make your entity readonly. Imho you should not have problems even if the table changes. If you need just two columns and nobody will change these two columns or create new required columns your mapped enity should still work. If somebody changes your column there will be no effective way to avoid breaking your code.

You can also use advanced EDMX features like QueryView (view in mapping description) and DefiningQuery (custom select in storage description) but those are probably overkill for your scenario.

Solution for the code-first: Turn off model metadata validation and database initialization. It requires setting initializer to null:

// Use this code in the application start up
Database.SetInitializer<MyContext>(null);

and removing IncludeMetadataConvetion:

public class MyContext : DbContext
{
    ...

    protedte override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();   
    }
}

In both approaches you can also use custom SQL query or custom linq projection.

Upvotes: 1

mateuscb
mateuscb

Reputation: 10710

When working with EF with medium to large groups, we opt for creating a database project, or simply a sql script to create the database/test data. With this file in source control, when a member of the team modifies the database everyone gets the latest script/project as well as the new edmx and all should work.

Upvotes: 0

Joel Brown
Joel Brown

Reputation: 14388

I haven't tried this, so I can't say that it will work with EF, but could you define a straight select view of the two columns that you want and maybe the primary key (if the primary key isn't one of your two columns) and then build your EF model against the view.

This would be how we would isolate a stable production system from ongoing changes to other columns in the good old days.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160852

Entity Framework is not really the ideal choice for a database schema that is still changing - after all the abstraction you are dealing with are entities that reflect a table schema, so if that is changing it will break EF. However nothing stops you from just using a store query to get your two columns and map it to a custom class that has properties matching the names of the columns you want:

class  MyColumns
{
  public string Column1 {get;set;}
  public string Column2 {get;set;}
}

...

using(var context = new FooEntities())
{
   var results = context.ExecuteStoreQuery<MyColumns>("select Column1, Column2 from MTable");
}

Upvotes: 1

Related Questions