entity-framework-core

EF Core column name from mapping api

纵饮孤独 提交于 2020-01-11 06:18:07
问题 In EF 6.1 the mapping API was introduced where we could finally get access to table names and column names. Getting the table name is a very nice change in EF Core, but I have not yet uncovered how to get the column names. For anyone interested here is how I got the table name in the latest version (RC1) context.Model.FindEntityType(typeof(T)).SqlServer().TableName What is the current method to get column names or is this not available yet? 回答1: var columnNames = ctx.Model.FindEntityType

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

孤街浪徒 提交于 2020-01-11 04:57:06
问题 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). 回答1: 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("ALTER TABLE SomeTable ADD CONSTRAINT CK_SomeTable_SomeColumn CHECK (SomeColumn

EFCore Not recognizing Database Provider

僤鯓⒐⒋嵵緔 提交于 2020-01-10 05:25:05
问题 I have a .Net Core WebApplication Project in which the Context Class is in a Class Library. If I hard code the connection string in the OnConfiguring(DbContextOptionsBuilder optionsBuilder) method I can generate migrations. Since it is better to let dependency injection manage the context I would like to add this to the Startup Class. However when I do I get the following error: No database provider has been configured for this DbContext. A provider can be configured by overriding the

EF Core - Many to many relationship on a class

陌路散爱 提交于 2020-01-10 02:57:20
问题 User-Friend relationship I find an answer Entity Framework Core: many-to-many relationship with same entity and try like this. Entitys: public class User { public int UserId { get; set; } public virtual ICollection<Friend> Friends { get; set; } } public class Friend { public int MainUserId { get; set; } public User ManUser { get; set; } public int FriendUserId { get; set; } public User FriendUser { get; set; } } The fluent API: modelBuilder.Entity<Friend>() .HasKey(f => new { f.MainUserId, f

DbSet doesn't have a Find method in EF7

风流意气都作罢 提交于 2020-01-09 04:52:53
问题 I am trying to create a generic repository to access my database. In EF6 I was able to do that in order to get a specific entity: protected IDbSet<T> dbset; public T Get(object id) { return this.dbset.Find(id); } DbSet in EF7 is missing a Find method. Is there a way to implement the above piece of code? 回答1: Here's a very crude, incomplete, and untested implementation of .Find() as an extension method. If nothing else, it should get you pointed in the right direction. The real implementation

Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

不想你离开。 提交于 2020-01-09 03:45:04
问题 After update to the new package Microsoft.EntityFrameworkCore.SqlServer 1.1.2 I got error when try to create DBContext: System.IO.FileLoadException occurred HResult=0x80131040 Message=Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Source

Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

此生再无相见时 提交于 2020-01-09 03:44:04
问题 After update to the new package Microsoft.EntityFrameworkCore.SqlServer 1.1.2 I got error when try to create DBContext: System.IO.FileLoadException occurred HResult=0x80131040 Message=Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Source

How do I get a property's original value while validating, in EF Core?

纵饮孤独 提交于 2020-01-07 05:16:06
问题 In EF Core, there is a neat way to perform entity validation using IValidatableObject . During validation I have the current value, but I also need access to the original value. This is the validation method: public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { } Is there some way to get access to the object's original values as well? I think the key is in ValidationContext but I'm not sure how. 来源: https://stackoverflow.com/questions/41771376/how-do-i-get-a

System.ArgumentNullException when using IncludeThen

不问归期 提交于 2020-01-07 01:49:25
问题 I have an Code First model: I am attempting to use Include() and IncludeThen() and get a System.ArgumentNullException . Here are the entities (let me know if you'd like more of the model): public class Area { public Area() { Geocode = new List<Geocode>(); } public int AreaId { get; set; } public int InfoId { get; set; } public string AreaDescription { get; set; } public string Polygon { get; set; } public string Circle { get; set; } public List<Geocode> Geocode { get; set; } public string

Generating code from an existing database EF7

a 夏天 提交于 2020-01-06 15:49:11
问题 I'm trying to generate code from my database but i get errors : System.AggregateException: One or more errors occurred . ---> Method not found this is my project.json: { "webroot": "wwwroot", "version": "1.0.0-*", "dependencies": { "Microsoft.AspNet.Mvc": "6.0.0-beta5", "Microsoft.AspNet.Server.IIS": "1.0.0-beta7", "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5", "Microsoft.AspNet.StaticFiles": "1.0.0-beta5", "EntityFramework.SqlServer": "7.0.0-beta8-15797", "EntityFramework.Commands":