Tag: ef code first

EF Core添加迁移调试

如何使用断点进入OnModelCreating并查看我的逻辑是否错误或者模型构建器是否正在做我不期望的事情? 我已经看过很多关于如何调试实际迁移的post,但没有关于如何观察模型代码生成方式的内容。 我正试图在我的一些实体上实现一些自定义属性,并且它被忽略了; 我想知道我的配置正在做什么,因为它正在生成模型代码。

Entity Framework是否自动保存相关类?

我们假设我们有这样的类 public class A{ string someField { get; set; } public virtual BB {get; set; } } public class B { int someIntField {get; set; } [ForeignKey(“Id”)] [Required] public virtual AA { get; set; } } 在代码中,我为它们创建了新实例,并建立了如下关系: A a = new A () { someField = “abcd”}; B b = new B () { someIntField […]

Code First Entity Framework – 更改连接字符串

如何在代码第一entity framework/ MVC应用程序中更改连接字符串? 我正在尝试将其转移到实际站点,但它忽略了Web配置值,仍然引用了我的本地版数据库。 这是我的web.config的连接字符串部分: 我不确定实体字符串是否有任何相关性,因为我使用Code Firstentity framework,我认为只有在我尝试创建edmx文件时才出现(尽管我最终只删除了它)。 实体连接字符串已被注释掉,所以我不认为它已被使用。 我希望entity framework读取“WebsiteConnectionString”,但它似乎想要使用本地连接字符串,但我甚至无法看到设置的位置。 我该如何改变它?

将EF CodeFirst Base类转换为Inherited类(使用table-per-type)

我正在使用EF Code First,并且有两个类定义如下: public class User { public int Id { get; set; } public string Username { get; set; } public string Email { get; set; } } [Table(“Visitors”)] public class Visitor : User { public Visitor() { Favourites = new List(); } public virtual IList Favourites { get; set; } } 这使用Table-Per-Typeinheritance并定义DB模式,如下所示: Users […]

使用Entity Framework的StructuralTypeConfiguration.Ignore()忽略除指定集之外的所有属性

在EF CodeFirst Fluent API中,我可以这样编写: modelBuilder.Entity() .Ignore(e => e.Property1); modelBuilder.Entity() .Ignore(e => e.Property2); 如何忽略所有的属性,但一小部分,如下: modelBuilder.Entity() .IgnoreAllBut(e => e.ID, e => e.Important); 是否可以编写像这样的IgnoreAllBut扩展方法?

WCF – 接收到http:// xxxxx / Service /的HTTP响应时发生错误

在你投票之前,我知道有一些重复的问题,比如这个问题,但是我试过并且没有成功找到它们的解决方案。 可能是我错过了一些东西,但我对WCF还是比较新的,所以有些东西让我头脑发热。 我在我的WCF服务中进行此调用: public User GetStudentRecord(string userName) { try { return new DashboardFacade().GetStudentRecord(userName); } catch (Exception ex) { ServiceFault fault = new ServiceFault(); fault.Operation = “GetStudentRecord”; fault.Reason = “Who knows…”; fault.Message = ex.Message; throw new FaultException(fault, fault.Message, new FaultCode(fault.Reason), fault.Operation); } } DashboardFacade()。GetStudentRecord(..)的定义如下: public User GetStudentRecord(string userName) { User user = ctx.Users.Where(x => x.ADUserName == […]

entity framework6代码优先 – 通过注释多对多的方式

是否有可能在entity framework6中创建具有代码优先和注释的单向多对多关联? 例: class Currency { public int id { get; set; } } class Country { public int id { get; set; } // How i can annotate this property to say EF that it is many-to-many // and it should create mapping table? // I don’t need navigation property to Country in Currency […]

如何在Entity Framework中设置两个相同类型的导航属性

使用代码第一个EF4(使用CTP5),我可以添加单个导航属性以及外键,它将遵循命名并且只将一次外键添加到表中。 如果我然后再添加相同类型的第二个属性,它会将其分解为表格中的4列而不是两列。 示例代码: 使用此模型,我获得了一个名为PressTypeID的PressType的AdapterFrameCapability表中的单个属性。 public class AdapterFrameCapability { [Key] public int AdapterFrameCapabilityID { get; set; } [Required] public int PressTypeID { get; set; } public virtual PressType PressType { get; set; } } 这是我想要建模的设置,但它会导致在表中创建4列,分别用于FromPressTypeID,FromPressTypeFromPressTypeID,ToPressTypeID和ToPressTypePressTypeID。 理想情况下,我只是喜欢FromPressTypeID和ToPressTypeID的列。 我在这做错了什么? public class AdapterFrameCapability { [Key] public int AdapterFrameCapabilityID { get; set; } [Required] public int FromPressTypeID { get; set; […]

如何使用entity framework在FluentAPI /数据注释中定义外键可选关系?

我有一个(示例)应用程序,代码如下: public class Posts { [Key] [Required] public int ID { get; set; } [Required] public string TypeOfPost { get; set; } public int PollID { get; set; } public virtual Poll Poll { get; set; } public int PostID { get; set; } public virtual Post Post { get; set; } } 基本上,我不知道是否有更好的方法来做到这一点,但是,我有一个post列表,并且,人们可以选择是否是Poll或Post ,因为entity […]

如何使Fluent API配置与MVC客户端validation一起工作?

我更喜欢使用Fluent API配置到DataAnnotation,因为我想将模型与数据访问分开。 我在MVC中尝试过,Fluent API不能与客户端validation一起使用。 简而言之,有一种简单的方法可以使Fluent API与客户端validation一起工作,因为DataAnnotation可以做到吗?