EF:部分类-访问适配器



假设我有一个表TabA

namespace MyProject.Models.Database //<-- the same namespace as the EF's dbmx file
{
    public partial class TabA
    {
        public void Foo()
        { 
            //
        }
    }
 }

Foo方法中,我需要对与TabA不相关的另一个表执行一些操作。换句话说,我需要访问该方法中的实体框架适配器。有可能吗?

编辑

答案就在这里https://stackoverflow.com/a/11135157/106616

如果我正确理解了这个问题,我认为您有自己的理由想要处理TabA实体中的另一个实体。如果这是真的,我可以看到两种方法。

A) 如果您希望您的更改与TabA实体的其他潜在更改同时应用,那么您可以始终将上下文作为参数传递:

namespace MyProject.Models.Database //<-- the same namespace as the EF's dbmx file 
{ 
    public partial class TabA 
    { 
        public void Foo(YourDbContext context) 
        {  
            var otherTableQuery = from x in context.SecondTable
                             where ...
                             select x;
            foreach (var item in otherTableQuery)
            {
                item.Column1 = "A certain value";
            }
        } 
    } 
}

您的调用方法可能看起来像:

public void DoChangesToTabA()
{
    using ( YourDbContext context = new YourDbContext())
    {
        var tabAquery = from x in context.TabA
                        where ...
                        select x;
        foreach( var item in tabAQuery)
        {
            item.LastModified = DateTime.Now;
            if(???)
            {
            }
        }
    }
} 

现在,您的更改将在下次调用上下文时应用。调用方法中的SaveChanges()。

最新更新