简体   繁体   中英

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!

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)

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 . So, I'd rather to pass an OBJECT value to the SAVE method than passing all the fields like above. 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:

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM