实体框架 Code First、工作单元、存储库和共享 DbContext

发布于 2024-12-03 23:03:07 字数 1966 浏览 1 评论 0原文

我正在使用 EF Code First 和 ASP.NET MVC 构建一个 Web 应用程序。我有以下类型:

IProblemRepository
EFProblemRepository
ICategoryRepository
EFCategoryRepository
CleanStreets // db context
IUnitOfWork
// etc.

代码片段:

public interface IUnitOfWork
{
    void Save();
}

public class CleanStreets : DbContext, IUnitOfWork
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<Point> Points { get; set; }
    public DbSet<Rating> Ratings { get; set; }
    public DbSet<Picture> Pictures { get; set; }
    public DbSet<Problem> Problems { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Category> Categories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
                    .HasMany(u => u.Comments)
                    .WithRequired(c => c.User)
                    .HasForeignKey(c => c.UserID)
                    .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

    public void Save()
    {
        this.SaveChanges();
    }
}


public class EFProblemRepository : IProblemRepository
{
    private readonly CleanStreets data;

    public EFProblemRepository(CleanStreets data)
    {
        this.data = data;
    }        

    public void Save(Problem problem)
    {
        if (problem.ProblemID == 0)
        {
            data.Problems.Add(problem);
        }

        data.Save();
    }
    ...
}

起初,我没有 UnitOfWork。我在每个存储库中创建了一个新的上下文。但是当我想要保存一个 ProblemProblem 包含 Category)时,使用上面提供的 Save 方法,收到以下错误:

一个实体对象不能被 IEntityChangeTracker 的多个实例引用

,我在 stackoverflow 上发现问题出在我的数据库上下文上,解决方案是使用工作单元模式创建一个共享上下文。我尝试这样做(如您在上面看到的),但我仍然收到错误。每次当我想要存储问题时,都会弹出错误。我是否实现了“共享”数据库上下文?

I'm building a web app using EF Code First and ASP.NET MVC. I have following types:

IProblemRepository
EFProblemRepository
ICategoryRepository
EFCategoryRepository
CleanStreets // db context
IUnitOfWork
// etc.

Code snippets :

public interface IUnitOfWork
{
    void Save();
}

public class CleanStreets : DbContext, IUnitOfWork
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<Point> Points { get; set; }
    public DbSet<Rating> Ratings { get; set; }
    public DbSet<Picture> Pictures { get; set; }
    public DbSet<Problem> Problems { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Category> Categories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
                    .HasMany(u => u.Comments)
                    .WithRequired(c => c.User)
                    .HasForeignKey(c => c.UserID)
                    .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

    public void Save()
    {
        this.SaveChanges();
    }
}


public class EFProblemRepository : IProblemRepository
{
    private readonly CleanStreets data;

    public EFProblemRepository(CleanStreets data)
    {
        this.data = data;
    }        

    public void Save(Problem problem)
    {
        if (problem.ProblemID == 0)
        {
            data.Problems.Add(problem);
        }

        data.Save();
    }
    ...
}

At first, I didn't have a UnitOfWork. I created a new context in every repository. But after I wanted to save a Problem (Problem includes Category), using the Save method provided above, I received the following error:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I found, on stackoverflow, that the problem is with my db context and the solution was to create a shared context with the unit of work pattern. I tried to do that (as you can see above) but I still get the error. Every time when I want to store a Problem the error pops. Did I implement a "shared" db context right?

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

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

发布评论

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

评论(1

浅语花开 2024-12-10 23:03:07

根据错误消息,您可能需要从一个上下文中分离对象以将其保存在另一个上下文中。为此,您还可以构造一个从第一个对象复制的新对象(此处需要深复制)。处理任何没有对象引用对应项的外键还需要额外的思考。

Based on the error message, you may need to detach an object from one context to save it in another. You could also construct a new object copied from the first (deep copy necessary here) in order to do that. There is also additional thought required to handle any foreign keys that don't have object reference counterparts.

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