简体   繁体   中英

EF6 Add an ObjectSet based on an existing ObjectSet

I have an EF6 ObjectContext containing a largish number of ObjectSets.

One EntityObject in particular has over a hundred properties, including some large text fields.

I have several forms to work with, that work with different pieces of the data.

Rather than have to load the entire object definition, is there a way I can define a "new" EntityObject that contains the primary key, timestamp, and a half dozen text fields, so that only those fields get pulled from the database?

I'm asking this because I've already got a databinding, validation and update pattern already built up for this, and I'd rather not have to build a second one to go alongside it.

I'm not sure if this is what you need, Hope it helps

1- Define a new class with the properties you want to store

2- write your sql/linq query to pull the fields you want from your db..

Say your table with over 100 properties is USERS table, now define another class

class TEMP_USERS{
    int U_ROWID {get;set;}
    string U_NAME {get;set;}
}

your query:

var query = "SELECT U_ROWID, U_NAME FROM USERS"

then store it in the defined class like this:

var db = new yourDbContext();
var data = db.Database.SqlQuery<TEMP_USERS>(query).ToList();

Note: selected query types and names should match with the defined class in order for this to work.

after you do this set your databindings accordingly but you will need to manually handle data validation and other stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM