Linq2SQL: Update object not created in datacontext

后端 未结 3 1703
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:01

Normally when you update an object in linq2sql you get the object from a datacontext and use the same datacontext to save the object, right?

What\'s the best way to

3条回答
  •  时光说笑
    2021-01-13 15:29

    I am hoping you can help. I am developing a tiered website using Linq to Sql. I created a new class(or object) in DBML designer called memberState. This object is not an actual table in the database. I have this method in my middle layer:

    public override IEnumerable(memberState) GetMembersByState(string @state)
    {
    using (BulletinWizardDataContext context = DataContext)
    {
    IEnumerable(memberState) mems = (from m in context.Members
    join ma in context.MemberAddresses
    on m.UserId equals ma.UserId
    join s in context.States
    on ma.StateId equals s.StateId
    where s.StateName == @state
    select new memberState
    {
    userId = m.UserID,
    firstName = m.FirstName,
    middleInitial = m.MiddleInitial,
    lastName = m.LastName,
    createDate = m.CreateDate,
    modifyDate = m.ModifyDate
    }).ToArray(memberState)();
    return mems; } }

    The tables in my joins (Members, States, and MemberAddresses are actual tables in my Database). I created the object memberStates so I could use it in the query above (notice the Select New memberState. When the data is updated on the web page how do I persist the changes back to the Member Table? My Member Table consists of the following columns: UserId, FirstName, MiddleInitial, LastName, CreateDate, ModifyDate. I am not sure how save the changes back to the database.

    Thanks,

提交回复
热议问题