简体   繁体   English

ASP.NET C#实体框架-如何正确更新外键? - 第2部分

[英]ASP.NET C# Entity Framework - How to update Foreign Key properly? - Part 2

This question is like a Part 2 of my previous one here! 这个问题就像我上一个问题的第2部分

Here are things I learned and hoping to be true: 这是我学到的并希望成为现实的事情:

  1. Do not use Data Entity Context as Static 不要将数据实体上下文用作静态
  2. Dispose the data context after used -usually after one time- 在使用后处理数据上下文-通常在一段时间后-
  3. Pass parameters into UPDATING Entity method instead of the Entity (which I'll explain and in here my crossroad lies) 将参数传递给UPDATING Entity方法而不是Entity(我将在这里解释,在这里我处于十字路口)

Here is my modified code: 这是我修改的代码:

public class ManagerBase
    {
        private NoxonEntities _entities = null;

        public NoxonEntities Entities
        {
            get
            {
                if (_entities == null)
                    _entities = new NoxonEntities();

                return _entities;
            }
        }
    }

//Managers uses ManagerBase class as a Base class
MemberManager currentMemberManager = new MemberManager();
currentMemberManager.Save(memberId, username, password, languageId);


//MemberManager class
public Member Save(long memId, string username, string password, int langId)
        {
            using (var Context =   base.Entities)
            {
                var Data = Context.Member.First(c => c.Id == memId);

                Data.Username = username;
                Data.Password = password;
                Data.Language = Context.Language.First(c => c.Id == langId); //Gets the foreign entity

                Context.SaveChanges();

                return Data;
            }
        }

This works without exception. 这毫无例外地起作用。 But normally this SAVE method is an implemented method by an interface . 但通常,此SAVE方法是通过接口实现的方法。 So, I'd rather to pass an OBJECT value to the SAVE method than passing all the fields like above. 所以,我宁愿一个对象值传递给save方法不是将所有的领域,如上述。 For example I'd like to use this: 例如,我想使用这个:

//Member is an EntityFramework Object which was created during EF Model when I added by visual studio
//Filter is the method that returns an EntityFramework Object from EF (and eventually database) with given some arguments
//SomeArguments could be  MemberId = 2
Member modifiedMember = currentMemberManager.Filter(someArguments);
modifiedMember.UserName = "newvalue";
currentMemberManager.Save(modifiedMember);

In the SAVE method perhaps I could use like this: 在SAVE方法中,也许我可以这样使用:

public Member Save(Member modifiedMember)
            {
                using (var Context =   base.Entities)
                {
                    var Data = Context.Member.First(c => c.Id == modifiedMember.Id);

                    Data.Username = modifiedMember.Username;
                    Data.Password = modifiedMember.Password;
                    Data.Language = Context.Language.First(c => c.Id == modifiedMember.LanguageId); //Gets the foreign entity

                    Context.SaveChanges();

                    return Data;
                }
            }

If I am gonna use this just like the one right above, I think I should NOT use the passing object EF Object and instead perhaps I should use some other class that I'll write just to map the parameters to the EF Member Entity. 如果我要像上面的右一个那样使用它,我想我不应该使用传递对象EF Object,而是应该使用我将要编写的其他一些类来将参数映射到EF Member Entity。

First question: Is this a good approach? 第一个问题:这是一个好方法吗?

public class MyMember
{
    public long Id { set; get; }
    public string Username { set; get; }
    public string Password { set; get; }
    public int LanguageId { set; get; }
}

MyMember.Username = "NewValue";
MyMember.LanguageId = 4; //4 as in new value
currentMemberManager.Save(MyMember); //As you can see MyMember is not an EF Entity in here

But this will take long time since there is already some written code. 但这将花费很长时间,因为已经有一些书面代码。

Second question: Can't I use an EF Entity object OUTSIDE and modify it there to save it IN the MemberManager class? 第二个问题:我不能境外使用的EF实体对象,然后修改它那里将其保存 MemberManager类?

Again, thank you very much for your help in advance. 再次感谢您的提前帮助。

Here is the answer: 答案是:

How to update entity? 如何更新实体?

And I thank you for the ones who helped me. 我感谢您为我提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET C#实体框架-如何正确更新外键? - ASP.NET C# Entity Framework - How to update Foreign Key properly? 没有实体框架外键关系的ASP.NET MVC - Asp.net mvc without entity framework foreign key relationships ASP.NET MVC如何访问Entity Framework生成的外键? - ASP.NET MVC How to access Entity Framework generated foreign key? 如何在 Entity Framework Core 和 ASP.NET Core MVC 中使用外键 - How work with foreign key in Entity Framework Core & ASP.NET Core MVC 如何使用asp.net实体框架在SQL外键中获取数据? - How to get data in an sql foreign key using asp.net entity framework? 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc 如何使用ASP.NET MVC中的Entity Framework将记录插入到具有外键的表中 - How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC 如何使用 LINQ 表达式 ASP.NET MVC Entity Framework 使用外键更新多个表 - How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework C#如何使用ASP.NET MVC中的实体框架将父数据和子数据插入/更新到sql数据库 - C# How to insert/update the parent and child data to the sql database using entity framework in asp.net mvc 如何使用 C# 中的实体框架更新具有外键关系的数据库中的记录 - How to update record in database with foreign key relationship using Entity Framework in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM