Tag: ef code first

将EF Code First应用程序部署到生产数据库

我在VS2010上使用代码优先方法编写了一个简单的.net mvc 3应用程序来生成模式。 当我将它部署到共享生产服务器时,我收到以下错误:[SqlException(0x80131904):在数据库’master’中拒绝CREATE DATABASE权限。 这是我的型号代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; namespace Bonappetit.Models { public class MenuItem { public int ID { get; set; } public string Title { get; set; } public string Description { get; set; } public decimal Price { get; set; } } public class MenuItemDBContext […]

如何在EntityFramework 4中保存子实体?

订单和联系人之间有1-1的关系。 即Contact.OrderId引用订单,也是PK。 所以我有一个现有的订单,我就像这样添加一个新的联系人…… order.Contact = new Contact() { EmailAddress = “hello” }; context.Orders.Attach(order); context.SaveChanges(); 发生了引用完整性约束违规:定义引用约束的属性值在关系中的主体和从属对象之间不一致。 那么我做错了什么?

entity framework代码优先 – 具有连接/链接表的一对多

是否可以与Code First创建一对多关系,在它们之间使用链接/连接表? public class Foo { public int FooId { get; set; } // … public int? BarId { get; set; } public virtual Bar Bar { get; set; } } public class Bar { public int BarId { get; set; } // … public virtual ICollection Foos { get; set; } } 我希望这个映射如下: TABLE […]

如何使用Entity Framework Code First Fluent API仅在连接表上级联删除

我正在尝试使用Entity Framework Code First Fluent API映射现有数据库。 我在与多对多关系和级联删除的情况下苦苦挣扎。 这是我的两个POCO课程: public class Foo { public int FooId; public string FooDescription; public IList Bars; } public class Bar { public int BarId; public string BarDescription; } 现在,我只有一个从Foo到Bar的导航属性。 那是因为在我真实的场景中,任何人从Bar到Foo都没有意义。 从Foo到Bar的关系实际上是0,n,这意味着Foo一个实例不需要Bar实例。 我现有的数据库在这两个表之间有一个连接表。 此表如下所示: create table Foo_Bar( FooId int not null, BarId int not null ); 因为我不想在我的项目中创建这样的POCO类,所以我映射了这样的表: modelBuilder.Entity() .HasMany(t => t.Bars) […]

使用数据注释创建外键

在下面的代码中,我需要在ParentInfoAddProperties.ParentQuestionAnswersId上设置一个外键constrant,以便它依赖于ParentQuestionAnswers.Id(这是一个主键)。 我试图使用数据注释,但entity framework6想要在我的ParentQuestionAnswers表中创建一个新的外键列,该列引用ParentInfoAddProperties.Id列而不是ParentInfoAddProperties.ParentQuestionAnswersId列。 我不希望Entity Framework创建新的外键列。 如果有人能够解释我应该指定哪些数据注释或(如果需要)流畅的映射来实现所需的外键实例,我将不胜感激。 提前致谢。 namespace Project.Domain.Entities { public class ParentQuestionAnswers { public ParentQuestionAnswers() { ParentInfoAddProperties = new ParentInfoAddProperties(); } [Required] public int Id { get; set; } [Required] public int UserId { get; set; } public ParentInfoAddProperties ParentInfoAddProperties { get; set; } } public class ParentInfoAddProperties { [Required] public int Id { […]

EF迁移中的自定义代码执行

在我们的项目中成功使用entity framework迁移。 但是现在我遇到了一个特殊情况,我需要更新一个需要一些业务逻辑的表(在我们的应用程序中作为C#代码)。 因此,我尝试在迁移Up方法中生成一个线程,并使用业务逻辑执行此表更新。 应用程序执行不需要此表更新,应该在后台进行。 我这样做有点像这样: public partial class MyMigration : DbMigration { public override void Up() { // … do some sql migration here … // after executing the sql migrations custommethod should run // migration seems to wait until CustomMethod finished work new Thread(() => ExecuteCustomMethodDatabaseContext()).Start(); } } 我希望Up方法在启动线程后返回,EF将MigrationHistory中的迁移设置为完成。 因此,应用程序可以启动,并在后台的某个地方更新表。 但事实并非如此,迁移似乎在线程运行时运行(这需要很多时间)。 所以我的相关问题: 一般来说,在DBmigrations中执行自定义代码是一种好习惯吗? […]

密钥列具有不同名称时实体拆分?

我正在使用Entity Framework 4.3.1 Code-First,我需要在两个表之间拆分实体。 这些表共享一个主键,它是1对1,但每个表上的列名称不相同。 我不控制数据布局,也不能请求任何更改。 例如,SQL表可以是 这将是我的实体…… public class MyEntity { public int Id {get; set;} public string Name {get;set} public string FromAnotherTable {get;set;} } 这是我的映射。 public class MyEntityMapping : EntityTypeConfiguration { public MyEntityMapping() { this.Property(e => e.Id).HasColumnName(“ThePrimaryKeyId”); this.Property(e => e.Name).HasColumnName(“MyDatabaseName”); this.Property(e => e.FromAnothertable).HasColumnName(“AnotherTableColumn”); this.Map(m => { m.Properties(e => { e.Id, e.Name }); m.ToTable(“MainTable”); […]

Code First无法启用迁移

我正在尝试启用迁移,但它正在抛出exception: 检查上下文是否以现有数据库为目标… System.TypeInitializationException:’System.Data.Entity.Migrations.DbMigrationsConfiguration`1’的类型初始值设定项引发exception。 —> System.TypeInitializationException:’System.Data.Entity.Internal.AppConfig’的类型初始值设定项引发了exception。 —> System.Configuration.ConfigurationErrorsException:配置系统初始化失败—> System.Configuration.ConfigurationErrorsException:必须在’section’标签上指定’name’属性。 我假设App.config文件没有正确设置(当我添加EF包时它自动设置)。 我所做的就是添加连接字符串: 我正在使用SQL Server 2008 R2。 因为我有一个连接字符串,我不相信我需要defaultconnectionfactory。 我对么? (注意:即使没有本节,我仍然会得到相同的例外) 我还缺少什么?

按entity framework中的ID获取元素列表

如何通过ID获取另一个列表中的所有元素? 例如; 我有List角色; 我想通过他们的ID获取此列表中数据库中的所有角色。 我正在使用代码优先。 我做了这个,它扔了一个错误: var roles = db.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.RoleId)); RoleId的类型为int。 错误: 无法创建“SampleMVC.Domain.Role”类型的常量值。 在此上下文中仅支持基本类型(例如Int32,String和Guid’)。

如何使用FK设置集合属性?

我有一个Business和Category模型。 每个Business通过公开的集合拥有许多Categories ( Category无视Business实体)。 现在这是我的控制器动作: [HttpPost] [ValidateAntiForgeryToken] private ActionResult Save(Business business) { //Context is a lazy-loaded property that returns a reference to the DbContext //It’s disposal is taken care of at the controller’s Dispose override. foreach (var category in business.Categories) Context.Categories.Attach(category); if (business.BusinessId > 0) Context.Businesses.Attach(business); else Context.Businesses.Add(business); Context.SaveChanges(); return RedirectToAction(“Index”); } 现在有几个business.Categories将其CategoryId设置为现有Category ( Category的Title属性缺失)。 […]