entity-framework-core

Entity Framework Core Customize Scaffolding

落爺英雄遲暮 提交于 2019-12-01 04:12:59
I have done scaffolding of my SQLServer database. And it creates the POCO objects in the specified folder. What i would like to do is that it extends from my base class. I also use repository pattern so i need to have Id key on every entity and I don't want to change that each time i rescaffold the database. Scaffold Model Example public partial class Food { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public double Price { get; set; } } Expected Result: public partial class Food : EntityBase { public string Name { get; set; } public

Is it possible to add CHECK constraint with fluent API in EF7?

偶尔善良 提交于 2019-12-01 04:02:40
Is it possible to add CHECK constraint with fluent API in Entity Framework 7? I need to acheive something like this: ... ADD CONSTRAINT CK_SomeTable_SomeColumn CHECK (SomeColumn >= X); It is ok if solution is provider-specific - I am targeting MsSqlServer only (at least now). As of EF 7.0.0-rc1, it isn't possible with the fluent API. You can define the constraint manually in the migration migrationBuilder.Sql("ADD CONSTRAINT CK_SomeTable_SomeColumn CHECK (SomeColumn >= X);"); Lukas Kabrt is still valid in 2019 so I had to create a special migration that I will include constraint and its drop:

Where should I put Database.EnsureCreated?

試著忘記壹切 提交于 2019-12-01 03:53:39
I have an Entity Framework Core + ASP.NET Core application and when my application starts up I want to ensure that the database is created, and eventually (once I have migrations) I want to ensure that those are also run. Initially I put Database.EnsureCreated() into the constructor of my DbContext but that appears to run every time someone hits my application since a new instance of the DbContext is created each time. I tried to put it into my startup code, but I need an instance of my DbContext to do that and it is unclear how exactly to get one. I am configuring EF as so: serviceCollection

Fluent Api Entity Framework core

蹲街弑〆低调 提交于 2019-12-01 03:48:28
问题 A user can have 1 or 0 account 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 { get; set; } } This is the fluent api code using Entity Framework 6 public class ClassDbContext: DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) {

Getting metadata in EF Core: table and column mappings

情到浓时终转凉″ 提交于 2019-12-01 03:43:14
Looking to get metadata in EF Core, to work with the mappings of objects & properties to database tables & columns. These mappings are defined in the DBContext.cs OnModelCreating() method, mapping tables with .ToTable(), and columns via .Property.HasColumnName(). But I don't see this metadata under the Entity Types returned by... IEnumerable<IEntityType> entityTypes = [dbContext].Model.GetEntityTypes(); Is this metadata available anywhere in EF Core? Ivan Stoev Is this metadata available anywhere in EF Core? Yes it is. Just additionally to the properties examine the methods ( GetXXX , FindXXX

Select specific properties from include ones in entity framework core

你离开我真会死。 提交于 2019-12-01 03:40:06
I use Entity Framework Core 2.0 and have this 2 classes: News public class News { public int Id { get; set; } public string Title{ get; set; } public string Text { get; set; } public DateTime ReleaseDate{ get; set; } public int AuthorID{ get; set; } public Author Author{ get; set; } } Author public class Author { public Author () { News = new List<News>(); } public int Id { get; set; } public string Username{ get; set; } public string Firstname{ get; set; } public string Lastname{ get; set; } public ICollection<News> News {get;set;} } I want to load the news with the author's username only

Is it possible to get complex Entity Framework objects from a REST api in .NET without creating ViewModel objects?

最后都变了- 提交于 2019-12-01 03:34:01
问题 Imagine a set of Entity Framework entities: public class Country { public string CountryCode { get; set; } public string Name { get; set; } public string Flag { get; set; } } public class Market { public string CountryCode { get; set; } public virtual Country Country { get; set; } public int ProductID { get; set; } public virtual Product Product { get; set; } } public class Product { public int ProductID { get; set; } public string Name { get; set; } public virtual ICollection<Market> Markets

How to implement IModelCacheKeyFactory in EF Core

痞子三分冷 提交于 2019-12-01 03:33:26
问题 The story: in our multi-tenant app (one PostgreSql db, multiple schemas) we need to use one DbContext against multiple schemas. What I tried: holding a cache (Dictionary, where key is schema name, value is the context for that schema). When instatiating new context for another schema I can see that dbContext schema is still set to previous schema provided. I assume the model in context is cached internally by context type, so that is the reason I see this behavior? So above doesn't seem to

Where is the EDMX

不想你离开。 提交于 2019-12-01 03:24:54
Getting exposed to dotnet Core. In a sample test application trying to setup EntityFramework.Core in dotnet core app. While I was able to add the EntityFramework.Core NugGet package I can't find the 'Add'->'New Item'->'Data'->'ADO.NET Entity Data Model' Is this not possible with EntityFramework.Core? How does EntityFramework.Core differ from EntityFramework 7? There is no edmx support in Entity Framework Core. It only supports a code-first approach. The option to add a new Entity Data Model will be added, but it will produce entity class files instead of an edmx file. The tooling development

Loading of references in EF7

会有一股神秘感。 提交于 2019-12-01 03:17:52
问题 I'm having two classes - author and blogpost: public class Author { public Author() { Blogposts = new HashSet<Blogpost>(); } public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Blogpost> Blogposts { get; set; } } and public class Blogpost { public Blogpost() { } // Properties public int Id { get; set; } public string Text { get; set; } public int AuthorId { get; set; } public Author Author { get; set; } } Using EF7 (beta4), I'm connecting them the following