ef-fluent-api

Entity Framework - Fluent APi - Create table with 2 FK

别来无恙 提交于 2019-12-30 14:09:34
问题 I have this product class which has a list of products associated. eg: Product = StarWar AssociatedProducts = Episode V: The Empire Strikes Back, Episode VI: Return of the Jedi, Episode VII: The Force Awakens But EF generates the database with some extra columns. This is my Product Class: 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;

Add Column Name Convention to EF6 FluentAPI

做~自己de王妃 提交于 2019-12-29 08:57:20
问题 This question was asked here 4 years ago: EF Mapping to prefix all column names within a table I'm hoping there's better handling these days. I'm using EF6 Fluent API, what I'll call Code First Without Migrations. I have POCOs for my models, and the majority of my database column names are defined as [SingularTableName]Field (e.g., CustomerAddress db column maps to Address field in Customers POCO) Table: CREATE TABLE dbo.Customers ( -- ID, timestamps, etc. CustomerName NVARCHAR(50),

how can create Unique Constraint with multi column with entityframework

你离开我真会死。 提交于 2019-12-25 18:59:06
问题 I'm use Entityframework Code First, and an EntityTypeConfiguration using fluent API. how can create Unique Constraint with multi column. for example i have a table with below field Id CompanyId Code Name i want set Code column to unique , per CompanyId 回答1: In your EntityTypeConfiguration you can do something like this: Property(m => m.CompanyId) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_YourUniqueIndexName", 1) { IsUnique = true })); Property(m => m.Code)

Fluent API One to Many mapping table

别来无恙 提交于 2019-12-25 06:55:39
问题 It's really helpful if anyone could explain me how to create a mapping (associated)table for one to many relationship using Fluent API.` public class Category { public int ID { get; set; } public string Name { get; set; } public List<Image> Images { get; set; } } public class Image { public int ID { get; set; } public string Name { get; set; } public List<Category> Category{ get; set; } The mapping table should contain CategoryID and ImageID. The solution should be something similar to this.

Loop over entity column mappings to transform column name

一个人想着一个人 提交于 2019-12-24 14:55:19
问题 I would like to apply a single transformation over a large number of columns in Entity Framework 5 without having to explicitly type them all out. As an example I would like to do the following over 50+ columns (convert PascalCase to UNDERSCORE_CASE). modelBuilder.Entity<Department>() .Property(t => t.DepartmentName) .HasColumnName("DEPARTMENT_NAME"); I found the Dapper.FluentMap which can provide this functionality but it doesn't appear to work when creating the query. Is there a way to loop

When an entity has more than one 0..1:n relationships to another entity, how to define them using Fluent API?

风流意气都作罢 提交于 2019-12-23 19:08:40
问题 I'm working on an Azure Mobile Service and in my model I have an entity Message , which is related to itself following this statement: A Message may have a parent Message , a Message may be the parent to many Messages . My class got defined as follows: public partial class Message { public Message() { this.Messages = new HashSet<Message>(); } public int Id { get; set; } public int CreatedById { get; set; } public int RecipientId { get; set; } public int ParentId { get; set; } public string

Entity Framework Core zero-or-one to zero-or-one relation

╄→尐↘猪︶ㄣ 提交于 2019-12-22 10:10:04
问题 Given these classes: public class A { public int Id {get; set;} public int? BId {get; set;} public B B {get; set;} } public class B { public int Id {get; set;} public int? AId {get; set;} public A A {get; set;} } Then with Fluent API modelBuilder.Entity<A>() .HasOne(a => a.B) .WithOne(b => b.A) .HasForeignKey<A>(a => a.BId); When creating objects and add them to database, things look like following in the corresponding tables: [A].BId is set [B].AId = null When I retrieve data using EF Core:

EntityFramework Code First FluentAPI DefaultValue in EF6.X

感情迁移 提交于 2019-12-21 06:47:05
问题 How can I set the default value using EntityFramework Code First FluentAPI for bool property? Something like: Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1); 回答1: Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax: AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false)); MSDN for "AddColumn" method 回答2: I'm not sure about a fluent way, but you can

Make EF use a m:n table with additional property [duplicate]

落爺英雄遲暮 提交于 2019-12-20 03:21:51
问题 This question already has answers here : Create code first, many to many, with additional fields in association table (6 answers) Closed 3 years ago . I ran into a problem when creating a new app. The app is about new employees that enter on a specific date. As they need some Hardware (such as Notebooks, keyboards etc.) we want to provide an overview over all the new employees that are going to come in the next few days. So there are the following Models: Entry: public class Entry{ public

Remove Auto Increment in EF Core 1.0 RC2 (former EF 7 RC2)

主宰稳场 提交于 2019-12-19 09:08:59
问题 In Entity Framework Core 1.0 RC2 (former Entity Framework 7 RC2), by default, all integer primary key are auto increment field. I tried everything to remove it. From using data annotation to fluent API, nothing works. Using data annotation: [Key, Column(Order = 1, TypeName = "INT"), DatabaseGenerated(DatabaseGeneratedOption.None)] Using fluent API: modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("DatabaseGenerated", DatabaseGeneratedOption.None); //OR use the following