English 中文(简体)
2. 创建多-2-曼字参照EF
原标题:Creating Many-2-Many reference with EF

在我目前的项目中(实际上很小),有3个表/POCO实体,希望利用EF加以操纵。

表格如下:

  1. Status (contains status details)
  2. StatusStatusType (which is needed because of the many-2-many relationship)
  3. StatusType (A table which is used to group statuses by type)

我现在想在数据库和用户代码中创造新的地位,如以下。

//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>/em>表。 我不想创造新的地位,只是一个参考。

我用的是:

status.StatusTypes == "new";

Update 22-04-2012 13:31

例子简化,在解决办法范围内跨越多个项目。 由于这一原因,在创建的章节(如意向)中不使用守则。 然而,我确实知道科索沃警察部队需要参考的地位。

最佳回答

如果你知道你的地位已经存在,你现在也必须是你的首要任务。 如果你具有主要关键价值,你可以采用这种办法:

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();
}

如果你不想在持久性守则中建立关系,那么你必须使用很少差别的方法。

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();
}
问题回答

暂无回答




相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签