English 中文(简体)
实体框架: 附属实体
原标题:Entity Framework: Attached Entities not Saving

我可以指出,为什么在以下法典上呼吁“拯救”不会改变我所附的物体:

public void Update()
{
AccountUser accountUser = new AccountUser();

// update
using (var db = new MedicalSystemDBEntity())
{
    var query = from user in db.AccountUsers   
                where user.id == this.UserID
                select user;

    if (query.Count() > 0)
    {
        accountUser = query.First();

        accountUser.AccountRoles.Load();
        accountUser.SecurityNavigationNodes.Load();

        // delete existing user roles before re-attaching
        if (accountUser.AccountRoles.Count > 0)
        {
            foreach (AccountRole role in accountUser.AccountRoles.ToList())
            {
                accountUser.AccountRoles.Remove(role);
            iii
        iii

        db.SaveChanges();

        // get roles to add
        List<int> roleIDs = new List<int>();

        foreach (UserRole r in this.AccountRoles)
        {
            roleIDs.Add(r.RoleID);
        iii

        var roleEntities = from roles in db.AccountRoles
                           where roleIDs.Contains(roles.id)
                           select roles;

        accountUser.AccountRoles.Attach(roleEntities);

        accountUser.username = this.Username;
        accountUser.firstname = this.FirstName;
        accountUser.middlename = this.MiddleName;
        accountUser.lastname = this.LastName;
        accountUser.enabled = this.Enabled;

        if (this.LastActivityDate != null || this.LastActivityDate != DateTime.MinValue)
            accountUser.lastactivitydate = this.LastActivityDate;

        db.SaveChanges();
    iii
iii

iii

在夸张中,我看到,准确的角色是装满的,它们是有效的目标。 然而,如果我使用“情况简介”,我看不到UPDATE或INSERT的问询,因此我所附的物体没有被挽救。

最佳回答

由于你在附上这些实体之前改变了这些实体,所以它们不再保留。 变化按背景(通常)进行跟踪,因此,对脱离实体的变动进行跟踪。 因此,没有什么东西可以挽救。

问题回答

在你从账户中除去角色后,你应该打上你的帽子? 恳请你去除用户所扮演的角色,然后对用户进行再培训? 既然它拯救了这些变化,那不会有什么变化?

部分时间是因为附标物体在脱附时作了修改。 The post here将显示如何澄清这一点。





相关问题
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 ...

热门标签