ef-code-first

Entity Framework Code First: How to map SQL Server's computed columns into CF model?

你。 提交于 2020-01-24 21:29:10
问题 Is it possible to map computed columns into Code First model? I'd like to define computed columns with SQL Server and then map them as properties inside my CF model. Thanks in advance Best Regards 回答1: Use the DatabaseGenerated attribute with DatabaseGeneratedOption.Computed as the value [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public string Foo { get; set; } 回答2: Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can

Foreign key with composite key in EF Core

巧了我就是萌 提交于 2020-01-24 10:17:30
问题 I have the following classes: public class ProductAttribute { public Guid ProductId { get; set; } public Guid AttributeId { get; set; } public List<ProductAttributeValue> Values { get; set; } public object[] GetKeys() { return new object[] {ProductId, AttributeId}; } } public class Product { public Guid Id { get; set; } public string Name { get; set; } } public class Attribute { public Guid Id { get; set; } public string Name { get; set; } } public class ProductAttributeValue { public Guid Id

EF Code First Not Generating Tables

被刻印的时光 ゝ 提交于 2020-01-23 05:38:05
问题 I'm working on an EF Code first site, and I've written my classes and a context class, the source of which is: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using MySite.SalesTool.Data.Entities; using System.Data.Entity.ModelConfiguration.Conventions; namespace MySite.SalesTool.Data { public class SalesToolEntities : DbContext { public DbSet<User> Users { get; set; } public DbSet<UserRole> UserRoles { get; set; } public DbSet

Entity Framework - relationship with fake Foreign Key (no foreign key in the db)

南楼画角 提交于 2020-01-22 14:14:28
问题 I have many tables that have TextID column which refers to the translation table. Translation table needs also LanguageID to get translated text in desired language. My problem is that I do not have LanguageID in my database, it is predefined in the system and I do not know how can I define it using Fluent API, i.e this can be my model: public partial class MyEntity { public short ID { get; set; } public Nullable<int> TextID { get; set; } [NotMapped] public Nullable<int> LanguageID { get; set

Entity Framework - relationship with fake Foreign Key (no foreign key in the db)

若如初见. 提交于 2020-01-22 14:13:45
问题 I have many tables that have TextID column which refers to the translation table. Translation table needs also LanguageID to get translated text in desired language. My problem is that I do not have LanguageID in my database, it is predefined in the system and I do not know how can I define it using Fluent API, i.e this can be my model: public partial class MyEntity { public short ID { get; set; } public Nullable<int> TextID { get; set; } [NotMapped] public Nullable<int> LanguageID { get; set

One-to-One relationships in Entity Framework 7 Code First

▼魔方 西西 提交于 2020-01-22 12:03:28
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

One-to-One relationships in Entity Framework 7 Code First

狂风中的少年 提交于 2020-01-22 12:02:44
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

One-to-One relationships in Entity Framework 7 Code First

旧巷老猫 提交于 2020-01-22 12:02:28
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

Possible to set column ordering in Entity Framework

≡放荡痞女 提交于 2020-01-22 08:18:36
问题 Is there any possible configuration to set database column ordering in entity framework code first approach..? All of my entity set should have some common fields for keeping recordinfo public DateTime CreatedAt { get; set; } public int CreatedBy { get; set; } public DateTime ModifiedAt { get; set; } public int ModifiedBy { get; set; } public bool IsDeleted { get; set; } I want to keep this fields at the end of the table. Is there any possible EF configuration that i can use to config this

Entity Framework Code First: How can I determine the foreign key property used for a navigation property at runtime?

蓝咒 提交于 2020-01-22 07:53:40
问题 I have an Entity Framework Code First DbContext with the following entities configured. In this example class Bar is a child of class Foo. public class Foo { public Guid Id { get; set; } public virtual ICollection<Bar> Bars { get; set; } } public class Bar { public Guid Id { get; set; } public Guid FooId { get; set; } public virtual Foo Foo { get; set; } } Now I know that internally, Entity Framework understands that the relationship between Foo and Bar is defined by the foreign key Bar.FooId