使用EF创建多对多引用 [英] Creating Many-2-Many reference with EF
问题描述
在我当前的项目中(这确实很小),我有3个表/ POCO实体,我想使用EF对其进行操作.
In my current project (which is really small) i have 3 tables/POCO entities which i would like to manipulate using EF.
表为:
- 状态(包含状态详细信息)
- StatusStatusType (由于多对多关系,因此需要使用)
- StatusType (用于按类型对状态进行分组的表)
- Status (contains status details)
- StatusStatusType (which is needed because of the many-2-many relationship)
- StatusType (A table which is used to group statuses by type)
我现在想在数据库和用户代码中创建一个新的状态,如下所示
I now want to create a new Status in the database and user code like you see below
//Create new status (POCO) entity
var newStatus = new Status {
StatusId = status.Id,
UserId = user.Id,
Text = status.text,
CreateDate = DateTime.Now
};
// Persist need status to database
using (var db = new demoEntities())
{
db.Statuses.AddObject(newStatus);
db.SaveChanges();
}
此代码工作正常,但我还想设置状态实体的 StatusType . StatusType 表中已包含所有可能的状态类型.我不想创建新状态,仅创建参考.
This code works fine but i want to also set StatusType of the status entity. All possible status types are already included in the StatusType table. I don't want to create new statuses only create a reference.
我认为我应该使用类似:
I figured i should use something like :
status.StatusTypes == "new";
更新22-04-2012 13:31
该示例已简化,并且涵盖了解决方案中的多个项目.因此,我不想在create部分中使用代码(例如demoEntities),但是我确实知道我需要引用的状态PK.
Update 22-04-2012 13:31
The example is simplified and spans multiple projects within a solution. Because of this i prefer not to use code within the create section (e.g. demoEntities).I do however know the PK of the status i need to reference.
推荐答案
如果您知道自己的状态类型已经存在,那么现在还必须为其主键.一旦有了主键值,就可以使用这种方法:
If you know that your status type already exists you also must now its primary key. Once you have a primary key value you can use this approach:
var newStatus = new Status {
StatusId = status.Id,
UserId = user.Id,
Text = status.text,
CreateDate = DateTime.Now
};
// Just dummy object for existing status type
var existingStatusType = new StatusType {
Id = existingStatusTypeId
};
// Persist need status to database
using (var db = new demoEntities())
{
db.Statuses.AddObject(newStatus);
// First let EF know that the status type already exists
// Attaching prior to making relation is important!
db.StatusTypes.Attach(existingStatusType);
// Now make relation between new and existing entity
newStatus.StatusTypes.Add(existingStatusType);
db.SaveChanges();
}
如果您不想在持久性代码内创建关系,则必须使用一些不同的方法.
If you don't want to create relation inside of the persistence code you must use little bit different approach.
var newStatus = new Status {
StatusId = status.Id,
UserId = user.Id,
Text = status.text,
CreateDate = DateTime.Now
};
// Just dummy object for existing status type
var existingStatusType = new StatusType {
Id = existingStatusTypeId
};
newStatus.StatusTypes.Add(existingStatusType);
// Persist need status to database
using (var db = new demoEntities())
{
// This will add both newStatus and existingStatusType as new entities
db.Statuses.AddObject(newStatus);
// You must fix it to make sure that existingStatusType is not inserted
// to database again
status.StatusTypes.ForEach(st =>
db.ObjectStateManager.ChangeObjectState(st, EntityState.Unchanged));
db.SaveChanges();
}
这篇关于使用EF创建多对多引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!