简体   繁体   中英

Entity Framework & MVC - Insert into multiple table using foreign keys

I have two tables, Property and PropertyMeta. They are linked by the PropertyID value. I have just started using ASP.net MVC and am currently using MVC2 (due to hosting provider contraints). From a simple create page i am trying to populate both tables but i'm not having much luck at all.

[HttpPost, Authorize]
    public ActionResult Create(PropertyViewModel property)
    {
        try
        {

            MyEntities db = new MyEntities();

            Property _property = new Property();
            _property = property.Properties;

            PropertyMeta _meta = new PropertyMeta();
            _meta.Property = _property;
            _meta = property.Meta;

            db.AddToProperties(_property);
            db.AddToPropertyMetas(_meta);
            db.SaveChanges();   

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

I have tried the above but it isn't working. It's annoying, this should be so simple but i'm stuck. Can anyone think what might be wrong with the above? Any help would be greatly appreciated!

Is PropertyViewModel.Properties of type Property ? I'm guessing the problem is in these 2 lines of code, since it doesn't seem to make sense to create a new Property and immediately assign property.Properties to it.

Property _property = new Property();
_property = property.Properties;

you are overwriting your value.

_meta.Property = _property; //sets an individual property
_meta = property.Meta; //Overwrites all properties

reverse the lines so you write all the values and then set the individual one after.

_meta = property.Meta; //Overwrites all properties
_meta.Property = _property; //sets an individual property

After reading your comment I realized what you were trying to do. Your property has no ID until after it has been entered into the DB. You can see this if you step through it in debugger. To fix this first submit the property before applying it to your meta.

        MyEntities db = new MyEntities();

        Property _property = property.Properties;
        db.AddToProperties(_property);
        db.SaveChanges();

At this point if you look in the debugger _properties ID value will now be set to the value assigned by the database. Now when you tie your meta to the property it will be an actual property item with a key so it will know how to associate it in the DB.

        PropertyMeta _meta = property.Meta;
        _meta.Property = _property;            
        db.AddToPropertyMetas(_meta);
        db.SaveChanges();

Your problem is not that Entity Framework is too complex, you seem to have a poor grasp of how C# works in general, given that you're creating new variables and immediately overwriting them with values of different types (not even sure how that compiles).

You have several problems:

Property _property = new Property(); 
_property = property.Properties; 

This creates a new _property. Then you assign a new Property over the one you just created. You shouldn't be using a domain model object in your viewmodel, but that's not really your problem.

You can just do this:

Property _property = property.Properties;

Next, you have this problem:

PropertyMeta _meta = new PropertyMeta();      
_meta.Property = _property; 
_meta = property.Meta; // overwrites the value assigned above

Again, you're overwriting the objects you just created. So this is equivelent:

PropertyMeta _meta = property.Meta;
_meta.Property = _property;

Finally, here's the last bit:

db.PropertyMetas.Add(_meta);             
db.SaveChanges();

EF will automatically add the Property to the table because it's linked in your _meta.Property table, so you don't add it explicitly.

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