[英]Entity Framework & MVC - Insert into multiple table using foreign keys
I have two tables, Property and PropertyMeta. 我有两个表,Property和PropertyMeta。 They are linked by the PropertyID value.
它们通过PropertyID值链接。 I have just started using ASP.net MVC and am currently using MVC2 (due to hosting provider contraints).
我刚刚开始使用ASP.net MVC,目前正在使用MVC2(由于托管提供商的约束)。 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
? 是
PropertyViewModel.Properties
类型的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.Properties
似乎没有意义。
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.
在将属性输入数据库之前,您的属性没有ID。 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. 此时,如果您在调试器中查看_properties,那么ID值现在将设置为数据库分配的值。 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). 您的问题不是Entity Framework太复杂了,因为您正在创建新变量并立即用不同类型的值覆盖它们(甚至不确定如何编译),所以您似乎对C#的总体工作掌握得不好。 。
You have several problems: 您有几个问题:
Property _property = new Property();
_property = property.Properties;
This creates a new _property. 这将创建一个新的_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. EF会自动将属性添加到表中,因为它已链接到_meta.Property表中,因此您无需显式添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.