C# 实体框架:此代码是最佳的吗?我该如何改进它?

发布于 2024-12-03 01:13:38 字数 1621 浏览 0 评论 0原文

我觉得下面的代码不是最理想的,它可以更好。我只是不知道如何在不提交更改的情况下获取密钥。

var now = DateTime.Now;
var update = new DAL.ServerStatus
{
    Accounts = status.Accounts,
    Clients = status.Clients,
    Created = now,
    ServerStarted = status.ServerStarted,
    ServerDateTime = status.ServerDateTime
};

var context = DataContext.GetDataContext();
context.ServerStatus.AddObject(update);
context.SaveChanges();

foreach (var character in characters)
{
    var characterUpdate = new DAL.ServerOnlineCharacter
    {
        Account = character.Account,
        Alliance = character.Alliance,
        Created = now,
        Criminal = character.Criminal,
        DonationPoints = character.DonationPoints,
        EventCredits = character.EventCredits,
        FactionTyped = character.FactionTyped,
        FactionPoints = character.FactionPoints,
        Fame = character.Fame,
        GameTime = character.GameTime,
        Guild = character.Guild,
        GuildAbbreviation = character.GuildAbbreviation,
        GuildTitle = character.GuildTitle,
        Karma = character.Karma,
        Kills = character.Kills,
        MapTyped = character.MapTyped,
        RaceTyped = character.RaceTyped,
        RawName = character.RawName,
        Serial = character.Serial,
        ServerStatusId = update.Id
    };

    context.ServerOnlineCharacters.AddObject(characterUpdate);
}

context.SaveChanges();

理想情况下,我希望这是事务性的,而不必两次访问数据库来完全提交更改。我怎样才能做到这一点?

顺便说一句,这是方法存根:

static void Update(IServerStatusUpdate status, IEnumerable<IServerOnlineCharacterUpdate> characters);

I feel like the following code is suboptimal, and that it could be better. I just don't know how to fetch the key without commiting the changes.

var now = DateTime.Now;
var update = new DAL.ServerStatus
{
    Accounts = status.Accounts,
    Clients = status.Clients,
    Created = now,
    ServerStarted = status.ServerStarted,
    ServerDateTime = status.ServerDateTime
};

var context = DataContext.GetDataContext();
context.ServerStatus.AddObject(update);
context.SaveChanges();

foreach (var character in characters)
{
    var characterUpdate = new DAL.ServerOnlineCharacter
    {
        Account = character.Account,
        Alliance = character.Alliance,
        Created = now,
        Criminal = character.Criminal,
        DonationPoints = character.DonationPoints,
        EventCredits = character.EventCredits,
        FactionTyped = character.FactionTyped,
        FactionPoints = character.FactionPoints,
        Fame = character.Fame,
        GameTime = character.GameTime,
        Guild = character.Guild,
        GuildAbbreviation = character.GuildAbbreviation,
        GuildTitle = character.GuildTitle,
        Karma = character.Karma,
        Kills = character.Kills,
        MapTyped = character.MapTyped,
        RaceTyped = character.RaceTyped,
        RawName = character.RawName,
        Serial = character.Serial,
        ServerStatusId = update.Id
    };

    context.ServerOnlineCharacters.AddObject(characterUpdate);
}

context.SaveChanges();

Ideally I'd want this to be transactional, and not having to go twice to the database to fully commit the changes. How could I achieve this?

This is the method stub by the way:

static void Update(IServerStatusUpdate status, IEnumerable<IServerOnlineCharacterUpdate> characters);

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

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

发布评论

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

评论(1

旧情勿念 2024-12-10 01:13:38

如果您将 update.Id 映射为主键,并且 ServerOnlineCharacter 和 ServerStatus 之间存在映射关系,那么您应该能够这样做,

characterUpdate.ServerStatus = update

而不是

characterUpdate.ServerStatusId = update.Id

这将允许您只需要在最后调用 context.SaveChanges (而不是在中间)。

至于使方法具有事务性,EntityFramework 遵守 TransactionScope,因此如果您将 Update 包装在 TransactionScope 周围并在准备好时提交它,它应该按您的预期执行。

这是事务使用的示例

如果您真的想要的话另外,您可能需要查看类似 AutoMapper 的内容来将您传递到 Update 的界面映射到你的 DAL 课程。这会减少很多代码。

If you have update.Id mapped as a primary key, and there's a relationship mapped between ServerOnlineCharacter and ServerStatus, then you should be able to do

characterUpdate.ServerStatus = update

instead of

characterUpdate.ServerStatusId = update.Id

This would allow you to only need to call context.SaveChanges at the end (and not in the middle).

As far as making the method transactional, EntityFramework abides by TransactionScope, so if you wrap your Update around a TransactionScope and commit it when you're ready, it should execute as you'd expect.

Here's an example of transaction usage

And if you really want to go the extra mile, you might want to take a look at something like AutoMapper to map your interface you're passing into Update to your DAL class. That would reduce a lot of code there.

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