N 层 LinqToSql 问题

发布于 2024-07-16 02:28:47 字数 1021 浏览 5 评论 0原文

我希望你能帮忙。 我正在使用 Linq to Sql 开发一个分层网站。 我在 DBML 设计器中创建了一个名为 memberState 的新类(或对象)。 该对象不是数据库中的实际表。 我的中间层有这个方法:

public override IEnumerable(memberState) GetMembersByState(string @state)
{
使用 (BulletinWizardDataContext context = DataContext)
{
IEnumerable(memberState) mems =(来自上下文中的 m.Members
将 ma 加入 context.MemberAddresses
m.UserId 等于 ma.UserId
在 context.States 中加入 s
ma.StateId 等于 s.StateId
其中 s.StateName == @state
选择新成员州
{
userId = m.UserID,
名字 = m.名字,
middleInitial = m.MiddleInitial,
姓氏 = m.姓氏,
createDate = m.CreateDate,
修改日期 = m.修改日期
}).ToArray(memberState)();
返回内存;
}
}

我的联接中的表(Members、States 和 MemberAddresses 是我的数据库中的实际表)。 我创建了对象 memberStates,以便可以在上面的查询中使用它(请注意选择新的 memberState。当网页上的数据更新时,如何将更改保留回成员表?我的成员表由以下列组成:UserId、FirstName、MiddleInitial、LastName、CreateDate、ModifyDate。我不知道如何将更改保存回数据库。

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱自由 2024-07-23 02:28:47

如果我没记错的话,您可以从不同的表(Members、States 和 MemberAddresses)创建一个视图,并将其添加到数据上下文中。 然后,可以保存对视图对象中数据的任何修改,并且只要在数据库和数据上下文中明确设置/定义所有关系,linq to sql 将正确处理提交。

If I remember correctly, you can create a view from the different tables (Members, States, and MemberAddresses) and add that to the data context. Then any modifications to data in the view object can be saved, and linq to sql will handle the commit correctly as long as all the relationships are clearly setup/defined in both the database and in the data context.

楠木可依 2024-07-23 02:28:47

如果您有一个 Member 表,则 dbml 很可能包含一个 Member 类。 要更新数据库中的成员,您必须创建一个新的 Member 对象,并将其附加到 BulletinWizardDataContext.Members 集合。 类似于以下代码的内容应该可以解决问题(我尚未测试该代码):

using (BulletinWizardDataContext context = DataContext)
{
    Member m = new Member() { UserId = userId };
    context.Members.Attach(m);
    m.FirstName = firstName;
    // Set other properties
    context.SubmitChanges();
}

在设置属性之前必须调用 Attach。 另外,当对象的属性设置为默认值(即数值为 0、布尔值为 false、字符串为 null 等)时,Linq2Sql 会出现一些 Attach 问题。 在这种情况下 Attach 将不会生成正确的 SQL。

If you have a Member table, the dbml will most likely contain a Member class. To update a member in the database, you will have to create a new Member object, and the Attach it to the BulletinWizardDataContext.Members collection. Something similar to the following code should the trick (I have not tested the code):

using (BulletinWizardDataContext context = DataContext)
{
    Member m = new Member() { UserId = userId };
    context.Members.Attach(m);
    m.FirstName = firstName;
    // Set other properties
    context.SubmitChanges();
}

Attach must be called before setting the properties. Also, Linq2Sql has some issues with Attach in the case where the properties of your object are set to default values (i.e. 0 for numeric values, false for booleans, null for string etc.). In this case Attach will not generate the correct SQL.

帥小哥 2024-07-23 02:28:47
var m = myContext.Members.Single(m=> m.UserID == myMemState.userID);
m.FirstName = myMemState.firstName;
m.MiddleInitial = myMemState.middleInitial;
...

那将是快捷的方法。 它对数据库进行了额外的往返,但效果很好。 如果这对您来说是个问题,那么请像雅各布建议的那样附加。 为此,您必须执行一些额外的步骤,例如检查乐观更新的配置,并确保在执行附加时具有原始字段。

var m = myContext.Members.Single(m=> m.UserID == myMemState.userID);
m.FirstName = myMemState.firstName;
m.MiddleInitial = myMemState.middleInitial;
...

That would be the quick way. It does an additional roundtrip to the db, but will work well. If that's an issue for you, then do Attach like Jakob suggested. For that you have to have to do some extra steps, like reviewing the configuration for optimistic updates and make sure you have the original fields when doing the attach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文