Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi anybody
I have two Entities(Asset and Owners). the relationship between them is many to many.
C#
public class Asset
   {
       [Key]
       public int AssetId { get; set; }
       public string AssetName { get; set; }

       public List<Owners> Owners { get; set; }
   }



C#
public class Owners
   {
       [Key]
       public int OwnerId { get; set; }
       public string Name { get; set; }

       public List<Asset> Assets { get; set; }
   }


Ok

I insert three assets that is "Computer", "Table" and "Tablet".

Now, I want insert a new Owner and assign some of the asset that already inserted to it.
I don't want insert a new asset, but also I want only select some asset for any owners that inserted. but I don't understand do it.


using (var context = new MyContext())
{
Owners newOwner = new Owners { Name = "Brad" };
newOwner.Assets = // here must be assign some of assets that already inserted.
context.SaveChanges();
}

thanks
Posted

1 solution

C#
Owners newOwner = new Owners { Name = "Brad" };
Asset newAsset = new Asset{ AssetName= "Blah"};

newOwner.Assets.Add( newAsset);
context.SaveChanges();
 
Share this answer
 
Comments
abbassep sep 3-Jun-14 1:31am    
No, I don't want insert new asset. I already inserted three assets. I want to insert new owner and assigned some assets to this owner.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900