Tag: ef code first

EF代码优先:inheritance的dbcontext创建两个数据库

我正在尝试创建一个基本dbcontext,其中包含所有常用实体,这些实体将始终在多个项目中重用,如页面,用户,角色,导航等。 这样做我有一个ContextBase类inheritanceDbContext并定义我想要的所有DbSet。 然后我有一个Context类inheritanceContextBase,我在其中定义项目特定的DbSet。 这些类定义如下: public class ContextBase : DbContext { public virtual DbSet Users { get; set; } //more sets protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new UsersConfiguration()); //add more configurations } } public class Context : ContextBase { public DbSet Buildings { get; set; } //some more project specific sets protected override void OnModelCreating(DbModelBuilder […]

实体Framewok代码首先使用本地SQL Server CE DLL“找不到ADO.NET提供程序”

我正在编写一个使用SQL Server CE 4.0文件的C#应用​​程序,这些文件可以通过代码优先通过Entity Framework 6.0访问。 (应用程序需要能够使用本地dll进行SQL Server CE连接,即应用程序需要可以部署XCOPY)。 应用程序在我的开发机器上运行良好,但在其他机器上(例如只有Win7和.NET 4.0的VM),我得到一个ArgumentException : 具有不变名称“System.Data.SqlServerCe.4.0”的ADO.NET提供程序未在计算机或应用程序配置文件中注册,或者无法加载。 有关详细信息,请参阅内部exception 内部exception消息说: 无法找到请求的.Net Framework数据提供程序。 它可能没有安装。 我搜索过Google和SO,大多数评论表明确保App.config文件正确无误。 我相信我的是(默认连接工厂和提供者部分),但这里是内容: build文件夹包含以下文件(当然,除了特定于应用程序的文件): EntityFramework.dll EntityFramework.xml EntityFramework.SqlServer.dll EntityFramework.SqlServer.xml EntityFramework.SqlServerCompact.dll EntityFramework.SqlServerCompact.xml 在每个amd64和x86子文件夹中都有以下文件: sqlceca40.dll sqlcecompact40.dll sqlceer40EN.dll sqlceme40.dll sqlceqp40.dll sqlcese40.dll 我确定该程序在开发机器上运行,因为已经安装了SQL Server CE,但是如何在其他机器上使用本地SQL Server CE dll来运行它?

EF 6 Code First存储过程 – 只读

我搜索了一些post,但是很短。 我首先使用EF6代码尝试从已在数据库中设置的存储过程中获取结果。 我的应用程序很简单,它从两个不同的服务器获取数据,执行一些业务逻辑,然后向用户显示。 我可以使用.edmx文件,它将函数映射到xml文件中。 当我使用Power Tools向我自己展示XML文件时,我没有看到我的代码中的函数导入,所以我要么缺少设置,要么我不能使用ExecuteFunction() 。 我可以先使用ExecuteFunction()代码吗? 我的存储过程只返回记录。 我有这个设置的原因是因为存储过程提供另一个应用程序,我们希望在一个地方(SSMS)保留对查询的所有更改。 我意识到我可以使用ExecuteStoredQuery / ExecureStoredCommand ,但是如果我使用.edmx模型,我想坚持惯例。 如果我可以使用ExecuteFunction ,我在哪里以及如何配置我的DbContext来识别存储过程? 在我的设置下面,我收到错误 在容器{1}中找不到FunctionImport {0} 我可以使用Fluent API吗? 谢谢。 public class JobContext : DbContext { public JobContext() : base(“name=JobContext”) { // My context will only define a slice of the database Database.SetInitializer(null); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.ComplexType(); } public […]

entity framework – 外键组件…不是类型的声明属性

我有以下模型 public class FilanthropyEvent : EntityBase, IDeleteable { public int Id { get; set; } public string Name { get; set; } public DateTime EventDate { get; set; } public string Description { get; set; } public decimal Target { get; set; } public decimal EntryFee { get; set; } public bool Deleted { get; […]

在entity framework代码第一种方法中映射字典

我有这样的字典: /// /// Gets the leave entitlement details. /// /// The leave entitlement details. public Dictionary LeaveEntitlementDetails { get; set; } 我想将它映射到数据库。 是否可以使用受保护或私有List ? 如: /// /// Gets the leave entitlement details. /// /// The leave entitlement details. public Dictionary LeaveEntitlementDetails { get; set; } public List LeveEntitlementStore { get { List leaveEntitlements = new […]

为什么Entity Framework返回空List 而不是空?

我是ASP .NET MVC世界的新手。 也许,这就是我无法向自己解释对我来说是一个令人讨厌的问题的原因。 我有一节有一对多的关系。 class MyClass{ public List otherClasses {get;set;} } 当我持久化这个类的一个实例时,我用空List 填充它的关系 MyClass myClass = new MyClass(){ otherClasses = new List() } context.myClass.Add(myClass); 问题是,当我尝试检索该实例时,由于任何原因,我尝试访问该列表,系统给我一个空参考例外… 我的问题是:为什么EF不返回空列表而不是空列表? 特别是在这种情况下,我用空列表坚持它? 有什么方法可以避免validation实例是否为null?

使用Entity Framework中的导航属性填充多对多关系表

我正在学习asp.net mvc应用程序中的Entity Framework。 我有3个型号 – AppModel,CategoryModel和App_CategoryModel(指定AppModel和CategoryModel之间的多对多关系)。 这个片段是: public class CategoryModel { [Key] public int id { get; set; } public string Name {get; set; } public virtual ICollection mapping { get; set; } } public class AppModel { [Key] public int id { get; set; } public string Name { get; set; } public virtual […]

EntityFramework Npgsql DbConfiguration不起作用

我试图用Npgsql配置EntityFramework,首先是代码。 EntityFramework 6.1.3 Npgsql 3.0.5 现在,我想通过类设置自定义配置,让我们看看: public class DbConfig : DbConfiguration { public DbConfig() { SetProviderFactory(“Npgsql”, Npgsql.NpgsqlFactory.Instance); SetProviderServices(“Npgsql”, provider: NpgsqlServices.Instance); SetDefaultConnectionFactory(new NpgsqlConnectionFactory()); } } 和我的上下文类: [DbConfigurationType(typeof(DbConfig))] public class Db: DbContext { public virtual DbSet Products {get;set;} public Db(DbConnection Connection): base(Connection, true) { } public Db(): base() { } public Db(string connectionString): base(connectionString) { } protected […]

EF Mapping为表中的所有列名添加前缀

我有一个大型的现有数据库,它有一个约定,其中所有的列名都以de-pluralised表名为前缀,例如 Tablename: addresses Columns: addressId addressLine1 addressLine2 是否有任何配置我可以应用于映射(使用流畅的API),我可以指定所有列名称都以字符串为前缀,例如“地址”? 否则我将需要定义每个列的映射,这看起来有点痛苦。 更好的是,如果我可以编写一个约定,其中前缀是从反复数表名自动设置的,但我知道此时不支持自定义约定,所以我想这更不可行。

类型’System.ObjectDisposedException’的exception

我有以下EF代码的第一个代码。 它工作正常。 但当我寻找’club2.Members’的价值时,它会说: ‘club2.Members’抛出了’System.ObjectDisposedException’类型的exception。 消息是: ObjectContext实例已被释放,不能再用于需要连接的操作。 我没有为Members属性设置值。 没关系。 但是它不应该在内部引起任何exception。 有没有办法克服这个例外? 删除此exception是否有助于提高性能? 为什么此exception不会导致程序执行终止? 注意:我不需要俱乐部实体中的会员数据(在我打电话的时候)。 即使当时没有成员加入俱乐部。 我只需要摆脱exception; 不使用预先加载来加载数据。 如何避免exception ? namespace LijosEF { public class Person { public int PersonId { get; set; } public string PersonName { get; set; } public virtual ICollection Clubs { get; set; } } public class Club { public int ClubId […]