Tag: ef fluent api

具有Entity Framework 6和PostgreSQL的Mono中不存在列Extent1.Id

我试图使用包Npsql和Npsql.EntityFramework映射到具有Entity Framework的PostgreSQL数据库。 我有一个包含下表的数据库: CREATE TABLE IF NOT EXISTS Crops ( Id INT PRIMARY KEY NOT NULL, Name TEXT NOT NULL, SowingMonths INT ); Crop模型如下所示: public class Crop { public int Id { get; set; } public string Name { get; set; } public Month SowingMonths { get; set;} //Month is an enum } 在数据库上下文中,我将模式映射到public,因为PostgreSQL中默认情况下所有表都在模式公共中。 protected […]

使EF使用am:n具有附加属性的表

我在创建新应用时遇到了问题。 该应用程序是关于在特定日期进入的新员工。 由于他们需要一些硬件(如笔记本,键盘等),我们希望提供未来几天内将要出现的所有新员工的概述。 所以有以下型号: 条目: public class Entry{ public IEnumerable Hardware {get; set;} … } 硬件: public class Hardware{ public string Name {get; set;} public OrderStatus Status {get; set} } 订单状态: public enum OrderStatus{ Nothing, Ordered, Arrived, Ready } 起初这似乎很好,因为对于每个条目我都想创建一个新的硬件数据集(一对多)。 现在我决定维护一个硬件列表,并将硬件引用到条目中(多对多)。 问题是OrderStatus绑定到硬件,因此当一个硬件被引用到许多Employees并且Orderstatus被更改时,它将在所有条目上更改。 我认为正确的解决方案是使用第三个表(Entry_Id,Hardware_Id,OrderStatus)。 使用流畅的API,可以在新(第三)表中映射关系,但不可能向其添加新属性(OrderStatus)。 这有可能吗?

如何以一对一/零关系指定外键?

我有一个父实体和一个子实体。 在DB中,父级的主键是p_p_id ,子级中的外键是相同的p_p_id 数据库中没有外键约束。 实体具有在各自的类中相互指向的属性。 家长class public virtual ChildProject ThisChildProject { get; set; } 儿童class public virtual ParentProject ThisParentProjection { get; set; } 这些属性上没有注释,也没有任何类的ID。 在配置中,我试图在孩子中进行映射。 HasRequired(i => i.ThisParentProject).WithOptional(o => o.ThisChildProject ); EF会尝试使用子键的主键和父键的主键进行映射。 但是我想在子节点中使用已定义的FK和父节点的主键

entity framework中的可选一对多关系

我在解决可选的一对多关系问题时遇到了问题。 我的模型是: public class Person { public int Identifier { get; set; } … public virtual Department Department { get; set; } } public class Department { public int Identifier { get; set; } … public virtual IList Members { get; set; } } 我想为一个Person分配零个或一个Department 。 分配后, Person应显示在Department的Members -List中。 我正在使用Fluent API配置Person ,如下所示: HasKey(p => p.Identifier); […]

entity framework – Fluent APi – 使用2 FK创建表格

我有这个产品类,其中包含相关产品列表。 例如: 产品= StarWar AssociatedProducts =第五集:帝国反击,第六集:绝地归来,第七集:原力觉醒 但EF使用一些额外的列生成数据库。 这是我的产品类别: public class Product { public int Id { get; set; } public string Name { get; set; } public string ShortDescription { get; set; } public string FullDescription { get; set; } public decimal UnitPrice { get; set; } …… public virtual ICollection AssociatedProducts { get; set; […]

删除EF Core 1.0 RC2中的自动增量(以前的EF 7 RC2)

在Entity Framework Core 1.0 RC2(以前的Entity Framework 7 RC2)中,默认情况下,所有整数主键都是自动增量字段。 我尝试了一切来删除它。 从使用数据注释到流畅的API,没有任何作用。 使用数据注释: [Key, Column(Order = 1, TypeName = “INT”), DatabaseGenerated(DatabaseGeneratedOption.None)] 使用流畅的API: modelBuilder.Entity().HasKey(t => t.ProdId).HasAnnotation(“DatabaseGenerated”, DatabaseGeneratedOption.None); //OR use the following modelBuilder.Entity().HasKey(t => t.ProdId).HasAnnotation(“DatabaseGenerated”, 0); //OR use the following modelBuilder.Entity().HasKey(t => t.ProdId).HasAnnotation(“Sqlite:Autoincrement”, false); 没有任何工作:( 你能帮我么? 更新 正如请求的那样,这是运行add-migration LocalDB_v1后得到的表脚本 migrationBuilder.CreateTable( name: “tblProduct”, columns: table => new { ProdId = […]

流利的Apientity framework核心

用户可以拥有1个或0个帐户 public class User { public int UserId { get; set; } public string Name { get; set; } public string Email { get; set; } public Account Account { get; set; } } public class Account { public int AccountId { get; set; } public DateTime CreatedDateTime { get; set; } public User User […]

entity frameworkFluent API不考虑基类属性

EF 6.1: 我们刚刚开始了一个有很多pfinheritance的项目。 选定的inheritancedb映射类型是每个层次结构的表。 问题是,在尝试使用add-migration生成迁移时,会引发以下错误: The foreign key component ‘VersionId’ is not a declared property on type ‘SER’. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. 以下是使用的类和配置类: public class Version : BaseObject { public virtual ICollection ListOfSER { get; set; } } public abstract […]

Ef core fluent api设置所有列类型的接口

不幸的是,ef核心不支持TPC模式,但我们需要这种行为。 我写了一个名为IBase的接口,每个实体都实现了这个接口: public interface IBase { Guid Id { get; set; } [Column(TypeName = “datetime2”)] DateTime CreateDate { get; set; } [Required] [StringLength(255)] string CreateUser { get; set; } bool Deleted { get; set; } } 我想摆脱Annotations以使用Fluent API配置。 但是我有大约20个不同的实体和7个Base-Values,我不想一遍又一遍地做出相同的配置: modelBuilder.Entity() .Property(e => e.CreateDate) .HasColumnType(“datetime2(2)”) .IsRequired(); 任何想法如何为实施IBase的所有实体配置一次Base-Property?

一个或零到一个entity framework代码First FluentApi

我需要创建一个或零个到一个引用的fluentapi,并在两个实体上都有导航属性。 EntityTwo应该包含存储外键的简单proerty(EntityOneId) public class EntityOne { public int Id { get; set; } public EntityTwo EntityTwo { get; set; } } public class EntityTwo { public int Id { get; set; } public int EntityOneId { get; set; } public EntityOne EntityOne { get; set; } } public class MyDbContext : DbContext { protected override […]