c# - Update Entity property in EF where property is another entity -


I am using unit framework 6 and I need to update the properties of an entity.

I have the following entities:

  Public class file {public Int32 ID {get; Set; } Receive public byte data; Set; } Public Date Time Update {get; Set; } Public virtual mm wax {get; Set; }} Public Class Mime {Public Int32 ID {get; Set; } The name of the public string {get; Set; } Public Virtual Icons & lt; File & gt; Files {get; Set; }}   

Then I used the following:

  _repository.update & lt; File & gt; (File, x => x.Data, x => X.Mime, x => x.Updated);   

The repository method is:

  Public Zero Update & lt; T & gt; (T unit, parameter expression & lt; funk & lt; t, object & gt; & gt; [] attribute) where t: class {_context.Set> & amp; T & gt; (). Attached (unit); Foreach (property in properties) (Member expression expression = property member member expression? (Member Experience) Property: Body: (Member Impact) ((Property of Property).) Operand;); _context.Entry & lt; T & gt; (unit). Property (expression .mail.name) .Ismodified = true;}}   

This data and works fine for updated properties But not for Mime . I get an error:

Property 'mime' type 'f Il 'is not a primitive or complex property. Property method can be used only with primitive or complex properties Use context or collection method.

Is it possible to do this work Is it integrated on my repository method?

Yes, I think it can be done The problem here is that I whenever a property is part of the table, or This is the shipping property, There is no easy way to check. Therefore it is difficult to call the right behavior.

If you are interested, take a look at the EF 6 source code, Internal Entrepreneur CAS - & gt; Property (..) which performs large amounts of property verification through metadata

The main idea is to basically scan your conceptual model, and whenever the property navigation property Determine (as the asset leads to another table), or if it is complex / primitive. > Accordingly, you say the right functionality.

  var property name = expression .mail Name; var attribute = __get_property_type __ (propertyName); If (propertyType == Property || propertyType == Complex) {_context.Entry & lt; T & gt; (Unit). Property (propertyName) .Ismodified = true; to continue; } If (propertyType == navigation) {// hm, should we attach it first?! // not sure that the first test must be dynamic underlying reference = unit. Gettype () .GetProperty (propertyName) .GetValue (unit, empty); _context.Entry (built-in reference) .set = EntityState.Modified; }   

To capture here, __ get_property_type __ that works is such that you work with the conceptual model, but it seems to me that It is not easy.

This is how we are working with reference property, EF6 detects:

  member member; EdmEntityType.Members.TryGetValue (property name, wrong, outside member); Var asNavProperty = Member Navigation Property; // if asNavProperty! = Null, we have the navigation property    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -