I don't need / need a key!

I have some views that I want to use EF 4.1 to query. These are specific optimized representations that do not have keys to talk about; there will be no deletions, updates, just a good choice.

But EF wants to set the key on the model. Is there a way to tell EF to move on, nothing to worry about?

More details

The main purpose of this is to request a set of views optimized for size, query parameters, and associations. The base tables have their own PC, FK, etc. It is indexed, staticized (what's the word?) And optimized.

I would like to have a class (this is a much smaller and simpler version of what I have):

public MyObject //this is a view { Name{get;set} Age{get;set;} TotalPimples{get;set;} } 

and a repository built from EF 4.1 CF where I can just

 public List<MyObject> GetPimply(int numberOfPimples) { return db.MyObjects.Where(d=> d.TotalPimples > numberOfPimples).ToList(); } 

I could open the key, but what is the real purpose of laying out 2 or 3 columns of the natural key? Will it never be used?

Current solution

It seems that their solution will not be EF CF, I have added a complex key to the model and I will expose it in the model. While it goes “with grain” to what can be expected from a “well-designed” db model, in this case IMHO, it added nothing but logic to the model builder, more bytes along the wire and additional properties per class. They will never be used.

+4
source share
3 answers

There is no way. EF requires a unique identification of a record key - an entity. This does not mean that you should provide an additional column. You can mark all your current properties (or any subset) as a key - this is exactly how EDMX does when you add a database view to the model - it goes through the columns and puts all non-empty and non-computed columns as primary key.

You should be aware of one problem - EF internally uses an identification card , and the object key is a unique identification on this card (each object key can be associated with only one example object). This means that if you cannot select a unique record identification and you upload several records with the same identification (your specific key), they will all be represented by an instance of one object. Not sure if this can cause any problems if you do not plan to modify these entries.

+2
source

EF is looking for a unique way to identify records. I am not sure that you can make him go against his nature to desire something unique in relation to objects.

But this is the answer to the question “Show me how to solve my problem, how I want to solve it”, and not actually solve your basic business requirements.

If this is "I do not want to show the user a key," then do not bind it when you bind data to your form (web or windows). If this is “I need to share these elements, but don’t want to give them the keys”, then match or surrogate the objects into an external domain model. Adds a bit of weight to the solution, but allows you to continue heavy lifting with a drag surface (EF).

The question is what is a business requirement that encourages you to create a bunch of objects without a unique identifier (key).

0
source

One way to do this is to not use views at all.

Just add the tables to your EF model and let EF create the SQL you are writing manually now.

0
source

All Articles