Tag: ef code first

entity framework外键作为主键代码优先

我有两个代码第一个模型,Foo和FooState,其中Foo有一个可选的FooState。 public class Foo { [Key] public int FooId { get; set; } public FooState FooState { get; set; } } public class FooState { [Key] public int FooStateId { get; set; } [Required] public int State { get; set; } [Required] public Foo Foo { get; set; } } 这工作正常,但是当我尝试将外键添加到FooState时 public class FooState { […]

对标有PhoneAttribute或UrlAttribute的字段允许空字符串

我正在使用CodeFirst Entitty框架5.我有一个代表用户的类。 public class User { [Key] public int UserId { get; set; } [Url] [DataType(DataType.Url)] [Required(AllowEmptyStrings= true)] public string WebSite { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Phone { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Fax { get; set; } } 我非常喜欢Phone和Url属性的validation机制,但遗憾的是,当标记有这些属性的字段是我实际想要允许的空字符串时,validation失败。 [Required(AllowEmptyStrings = true)]似乎不适用于Phone或Url属性。 这似乎适用于其他一些DataAnnotations属性,如EmailAddress […]

在WPF中使用EF Code First和SqlCe

我正在尝试在我的WPF应用程序中使用EF Code First,我的想法是在AppData / MyApp中创建一个SqlCe Db(如果没有),并将其与EF Code First一起使用。 当我尝试从它应该创建的数据库中读取数据时,它正在给出错误,但是当我检查数据库上下文对象时,我看到它正在尝试在SqlExpress中创建它。 首先,我如何设置它与CE而不是SqlExpress一起使用并设置文件位置? 我尝试更改app.config中的连接字符串,但无法使其工作(它没有创建sdf文件),而且我不知道如何设置连接字符串路径到AppData文件夹,因为它在用户文件夹中(不固定)。 从未使用过SqlCe或EF Code First,所以欢迎任何帮助和赞赏。 提前致谢。

entity framework代码优先 – 将两个字段联合成一个集合

我有这个型号和配置 public class Person { public int? FatherId { get; set; } public virtual Person Father { get; set; } public int? MotherId { get; set; } public virtual Person Mother { get; set; } public virtual List Childs { get; set; } } class PersonConfiguration : EntityTypeConfiguration { public PersonConfiguration() { HasOptional(e => e.Father).WithMany(e […]

entity framework,代码优先。 调用时不会填充子对象

我首先要掌握EF代码。 当我在代码中调用它时,我的域模型设计似乎不支持对象的自动“填充”子项。 模型: public class Car { [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required,MaxLength(10)] public string Registration { get; set; } [MaxLength(30)] public string Make { get; set; } [MaxLength(45)] public string Model { get; set; } [Required] public Coordinates Coordinates { get; set; } [Required] public Client Client { get; set; } […]

在移动服务数据库上启用代码优先迁移时出错

我有一个Azure移动服务项目(C#后端),我最近创建并附加到Azure SQL数据库。 我一直在尝试在该后备数据库上启用代码优先迁移,但在尝试更新数据库时会抛出错误。 我执行了所有常规步骤以启用迁移(启用迁移,添加迁移)。 但是当我尝试Update-Database时,它会返回以下错误: 无法在表’dbo.Appointments’上创建多个聚簇索引。 在创建另一个之前删除现有的聚簇索引“PK_dbo.Appointments”。 为什么会这样? 我的数据库中没有任何表,该项目几乎是默认的。

在代码第一entity framework中定义多对多关系

我在模型中有3个课程,如下所示。 [Table(“UserProfile”)] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string UserName { get; set; } public ICollection MartialArtUserProfiles { get; set; } } [Table(“MartialArt”)] public class MartialArt { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Name { get; set; } public string Description { […]

entity framework5,首先从代码切换到数据库?

我已经使用代码创建了一个数据库。 它运作良好。 此后,在Visual Studio外部对数据库进行了许多更改。 如此多的迁移尝试都会产生错误。 所以我的问题是,有没有办法首先将解决方案从代码切换到数据库?

如何在EF6 Code First中使用generics类型与数据库上下文

例如,假设我有4个不同的实体,每个实体都实现一个将实体添加到数据库的Add()方法: public class Profile { … public void Add() { this._dbContext.Profile.Add(this); this._dbContext.SaveChanges(); } … } 现在我希望有一个generics类在一个抽象类而不是X个类中实现这种行为。 所以我尝试了以下内容: public abstract class Entity where TEntity : class { protected DbContext _dbContext; protected Entity() { this._dbContext = new SMTDBContext(); } public void Add() { this._dbContext.Set().Add(this); this._dbContext.SaveChanges(); } } 当然它并不是因为“这个”不是一个TEntity ……但它将在未来! 到目前为止,我试图寻找做类似事情的人没有成功。

EF Code First阻止使用Fluent API进行属性映射

我有一个Product类和一个复杂类型的AddressDetails public class Product { public Guid Id { get; set; } public AddressDetails AddressDetails { get; set; } } public class AddressDetails { public string City { get; set; } public string Country { get; set; } // other properties } 是否可以防止从Product类中的AddressDetails映射“Country”属性? (因为我永远不需要它用于Product类) 像这样的东西 Property(p => p.AddressDetails.Country).Ignore();