Using Entity Framework and having the following class:
public class TestClass{
[Key]
public int id {get; set;}
public int foreignId {get; set;}
[ForeignKey("foreignId")
public MyObject myObject {get; set;}
}
With this class I want to save the "TestClass" but the entity framework first tries to save/create the "myObject" that doesn't need to get saved because it's only a reference. In some forums ( enter link description here ) I read that I has to set the myObject explicitely to "null" before saving, but in my opinition this is quite annoying.
So I want to ask if there is any annotation or something like "IgnoreOnSave" that I can add to the myObject?
[ForeignKey("foreignId", "ignoreOnSave")]
public MyObject myObject {get; set;}
Update
[NotMapped]
did it for me:
[NotMapped]
[ForeignKey("foreignId")]
public MyObject myObject {get; set;}
With this attribute I can load the object with the foreign sub object, but on saving the subObject "myObject" is ignored. I only need to assure in code, that the ID of "myObject" is copied to the foreign-key-property of the main object.
Additionally (I'm creating a Web-Api-Solution) I added a [JsonIgnore]
to the foreign-key-property so that I have clean interfaces.
Update
Using [NotMapped]
leads to that the linq expression ".include" can't be used for the respective properties anymore.
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.