标签: entity-framework-4

实体框架4.0 GetChanges()等效

在LINQ to SQL中,您可以覆盖SubmitChanges并使用方法this.GetChangeSet()来获取所有插入,更新和删除,以便您可以在将最后一分钟更改提交到数据库之前进行更改.

这可以在EF 4.0中完成吗?我看到有一个覆盖SaveChanges,但我需要知道GetChangeSet()的等价物

entity-framework entity-framework-4

8
推荐指数
1
解决办法
4294
查看次数

使用Entity Framework 4以编程方式读取外键元数据

有谁知道如何从edmx生成的实体框架中获取架构信息?

具体来说,我要设法穿越了,我目前没有实例的实体的外键,并获得它的外键关系,我想通过反射在会一般适用于任何一种方式来做到这一点每次都没有自定义代码的实体类.

EG:我的架构有2个类,User和Group.我有数字"42",我知道,从"用户"实体"的GroupId"属性来了,但此刻我不能工作,如何检测到的"用户"外键本"的GroupId"属性"Group"实体由它的"GroupId"属性组成.

c# entity-framework-4

8
推荐指数
2
解决办法
9989
查看次数

在Entity Framework中是否有与NHibernate的<component>相同的东西?

我们正在研究我们可以在项目中使用哪种ORM技术,更具体地说是NHibernate与实体框架4.

NHibernate的一个好处是可以将多个列映射到自定义类型,这要归功于"组件"映射选项.也就是说,我可以将一组列映射为给定指定类型的属性,如下所示:

    Component<MyCustomType>(e => e.CreatedBy,
        p =>
        {
            p.Map(customTypeItem => customTypeItem .prop1, "column1");
            p.Map(customTypeItem  => customTypeItem .prop2, "column2");
        });
Run Code Online (Sandbox Code Playgroud)

我还没有在Entity Framework 4中找到类似的功能.它是否存在?或者是否有类似的功能?

nhibernate fluent-nhibernate entity-framework-4

8
推荐指数
1
解决办法
1600
查看次数

实体框架4 CTP 5自我引用多对多

我的数据库中有以下场景.这是一项研究记录,这些研究将其他研究作为先决条件.在我的数据库设计中,它看起来像这样:

自我引用多对多

我的代码看起来像这样:

public class Study
{
    public int ID { get; set; }
    public string Topic { get; set; }
    public byte TypeID { get; set; }
    public virtual StudyType Type { get; set; }
    public bool Deprecated { get; set; }

    public virtual ICollection<Study> Prerequisites { get; set; }
}

public class StudyType
{
    public byte ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Study> Studies { get; set; }
}

public class …
Run Code Online (Sandbox Code Playgroud)

many-to-many entity-framework-4 self-reference entity-framework-ctp5

8
推荐指数
1
解决办法
1635
查看次数

为什么将EntityState设置为Detached清空List <T>类型的属性?

使用实体框架代码首先,我有类似的东西:

public class Foo
{
    public int Id { get; set; }

    public List<Bar> Bars { get; set; }
}    

Foo foo = (from f in ctx.Foos.Include("Bars") where f.Id == 42 select f).Single();

// At this point foo.Bars is populated

ctx.Entry(foo).State = EntityState.Detached;

// At this point foo.Bars is an empty List
Run Code Online (Sandbox Code Playgroud)

为什么分离一个对象导致它的属性public List<string> Bars(明确且成功地包含在内)被清空?

分离可能具有许多属性的对象的正确过程是什么?

entity-framework-4 ef-code-first

8
推荐指数
1
解决办法
5121
查看次数

如何修复"在应用程序配置中找不到连接名称'LocalSqlServer'或连接字符串为空." 错误?

我有一个使用SQL Server CE 4.0和Entity Framework 4.0的ASP.NET项目.它在我的本地计算机上工作正常.

但是,当我将它移动到远程服务器,并尝试登录到程序的管理部分时,我收到以下错误:

The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
Run Code Online (Sandbox Code Playgroud)

它引用了远程服务器上machine.config中的以下行:

Line 237:    <membership>
Line 238:      <providers>
Line 239:        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
Line 240:      </providers>
Line 241:    </membership>
Run Code Online (Sandbox Code Playgroud)

现在,我的web.config中没有"AspNetSqlMembershipProvider"语句,它引用了一个名为"LocalSqlServer"的连接字符串.相反,我有以下内容:

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider" connectionStringName="PUGConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
Run Code Online (Sandbox Code Playgroud)

这实际上在本地工作.连接字符串的名称是PUGConnection,而不是LocalSqlServer. …

asp.net asp.net-membership membership-provider sql-server-ce entity-framework-4

8
推荐指数
1
解决办法
1万
查看次数

处理DbContext后出现问题

我最近更改了我的MVC3应用程序,试图正确处理DbContext对象[1].这在开发中非常有用,但是一旦应用程序被推送到我的生产服务器,我就开始间歇性地获得一些有趣的例外,这些例外会一直存在,直到AppPool被回收.异常可以追溯到我的自定义中的代码AuthorizeAttribute,如下所示:

System.InvalidOperationException: The 'Username' property on 'User' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'String'.

System.InvalidOperationException: The 'Code' property on 'Right' could not be set to a 'String' value. You must set this property to a non-null value of type 'Int32'. 
Run Code Online (Sandbox Code Playgroud)

(数据库模式如下所示:用户:[Guid,String,...],权利:[Guid,Int32,...])

就好像某些"电线越过",应用程序混合了数据库的Right结果:试图将结果表示为a User,反之亦然.

为了管理处理DbContext,我把代码放在每个控制器级别存储.当处理控制器时,我也将其丢弃DbContext.我知道它很hacky,但是AuthorizeAttribute使用相同的上下文filterContext.Controller.

处理DbContext这个庄园的物体生命周期有什么问题吗?关于为什么我得到上述十字交叉的例外,是否有任何合理的解释?

[1]虽然我知道没有必要处理DbContext对象,但我最近遇到了许多消息来源,说明这是最好的做法.

编辑(按@ MikeSW的评论) …

dispose entity-framework-4 asp.net-mvc-3 dbcontext

8
推荐指数
1
解决办法
764
查看次数

由于一个或多个外键属性不可为空,因此无法更改关系

(注意:即使它有相同的例外,这也不是这个问题的重复.)

我有一个穷人的交易策略是:

  1. 插入父记录和子记录.
  2. 执行长时间运行.
  3. 如果长时间运行操作失败,请删除先前插入的父记录和子记录.

当我尝试第3步时,收到以下消息:

操作失败:无法更改关系,因为一个或多个外键属性不可为空.当对关系进行更改时,相关的外键属性将设置为空值.如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象.

我一般都明白这意味着什么,但我认为我是按照规则玩的,无论我怎么努力遵守规则,我都不确定为什么我会得到这个消息.

我们使用自我跟踪实体,我的代码实际上是这样的:

var parent = new Parent(1,2,3);
var child = new Child(4,5,6);
parent.Children.Add(child);

MyContext.Parents.ApplyChanges(parent);
MyContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

// At this point, inserts were successful and entities are in an Unchanged state.
// Also at this point, I see that parent.Children.Count == 1

var shouldDeleteEntities = false;
try
{
  // This is not database-related. This process does some
  // encryption/decryption and uploads some files up to
  // …
Run Code Online (Sandbox Code Playgroud)

c# sql-server .net-4.0 entity-framework-4

8
推荐指数
1
解决办法
9616
查看次数

使用Entity Framework Power Tools逆向工程时自定义上下文文件名

当我使用EF Power Tools逆向工程生成上下文时,它在我的数据库名称的文件中创建了一个带有我的数据库名称的类.我能够修改T4模板以使用Context类名称的静态名称.但是,我想在类的文件名中使用相同的静态名称.我怎样才能做到这一点?

t4 entity-framework-4

8
推荐指数
1
解决办法
955
查看次数

使用实体框架缓存和延迟加载

假设我有一个应用程序,例如一个网站,我的objectcontext在请求期间离开.我应该缓存一些使用EF加载的数据,以避免读取数据库并提高性能.

好吧,我用EF读取数据,我将对象放在缓存中(说AppFabric,而不是内存缓存),但是可以延迟加载的相关数据现在为空(对此属性的访问会导致nullreferenceexception).我不想在一个请求中加载所有内容,因为它会太长,所以我希望按需加载,一旦读取,我想用新获取的数据完成缓存.

注意 :

  • 只读操作,没有创建/更新/删除.
  • 不想使用Jarek Kowalski制作的"EF Provider Wrappers"等二级缓存

我怎样才能做到这一点 ?

编辑:我用northwind数据库构建了这个样本,它正在工作:

class Program
{
    static void Main(string[] args)
    {
        // normal use
        List<Products> allProductCached = null;
        using (NORTHWNDEntities1 db = new NORTHWNDEntities1())
        {
            allProductCached = db.Products.ToList().Clone<DbSet<Products>>();
            foreach (var product in db.Products.Where(e => e.UnitPrice > 100))
            {
                Console.WriteLine(product.ProductName + " => " + product.Suppliers.CompanyName);
            }
        }

        // try to use cache, but missing Suppliers
        using (NORTHWNDEntities1 db = new NORTHWNDEntities1())
        {
            foreach (var product in allProductCached.Where(e => e.UnitPrice > 100)) …
Run Code Online (Sandbox Code Playgroud)

c# caching entity-framework-4

8
推荐指数
1
解决办法
7705
查看次数