entity-framework-core

Update Database after Model Changes - Entity Framework 7

人盡茶涼 提交于 2019-12-01 03:08:02
问题 I have created an app using the lastest ASP.NET5 MVC 6 Entity Framework 7 and setup migrations using dnx . ef migration add Initial dnx . ef migration apply This works but when I make a change to the model the database is not updated. I would like to have the database automatically update after a model change when I run the program. My research only points me to old information that doesn't seem to be appropriate to Entity Framework 7. My current code: public ApplicationDbContext(): base() {

DbFunctions.TruncateTime LINQ equivalent in EF CORE

感情迁移 提交于 2019-12-01 02:56:54
I have the following functioning LINQ in my .net app public ActionResult Index() { Dictionary<DateTime?, List<Event>> result; result = (from events in db.Events.Include("Activity") where events.IsActive group events by DbFunctions.TruncateTime(events.DateTimeFrom) into dateGroup select new { EventDate = dateGroup.Key, Events = dateGroup.ToList() }).ToDictionary(x => x.EventDate, x => x.Events); return View(result); } When I use this in EF Core, I can't use DbFunctions. How can I rewrite this to make it work in Microsoft.EntityFrameworkCore ? I am using SQLite if that makes a difference. In EF6

EFCore nullable relationship setting onDelete: ReferentialAction.Restrict

时光怂恿深爱的人放手 提交于 2019-12-01 02:54:46
I'm running efcore 2.0.1. I have a model: public class BigAwesomeDinosaurWithTeeth { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public ICollection<YummyPunyPrey> YummyPunyPrey { get; set; } } public class YummyPunyPrey { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public Guid? BigAwesomeDinosaurWithTeethId { get; set; } [ForeignKey("BigAwesomeDinosaurWithTeethId")] public BigAwesomeDinosaurWithTeeth BigAwesomeDinosaurWithTeeth { get; set; } } I have no fluent api on these two classes. But when I generate

Migrating from EF Core 2 to EF Core 3

天大地大妈咪最大 提交于 2019-12-01 02:46:52
问题 After upgrading my project from (dotnet core 2/ef core 2) to (dotnet core 3/ef core 3) almost all of my entity framework LINQ queries are broken. while I already read this it's still unclear to know what to do. Here are some examples that I have problem with: var league = await dbContext.League.LastAsync(); While this code worked just fine in ef core 2 it throws exception in ef core 3. The only workaround I could find about this was the following code which is still not what I want since it's

Asp.net core 2.1 crashing due to model InverseProperty

百般思念 提交于 2019-12-01 02:37:52
问题 My site is crashing on load with an extremely long unhandled exception in System.Reflection on the first call to the DbContext. Using ef-core 2.1 rc1 System.ArgumentNullException: Value cannot be null. Parameter name: type at System.Reflection.IntrospectionExtensions.GetTypeInfo(Type type) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.InversePropertyAttributeConvention.ConfigureInverseNavigation(InternalEntityTypeBuilder entityTypeBuilder, MemberInfo navigationMemberInfo,

How to configure an Identity column using Entity Framework Core?

℡╲_俬逩灬. 提交于 2019-12-01 02:10:52
How do I create an Auto increment identity column in Entity Framework Core? Obviously I can do it using fluent API for EF6 for example. Since there is very little EF7 documentation, much of what we know we have to glean from the source or unit tests. According to the following two unit tests in the EF7 source... Here and Here You would configure a property for Identity like this: b.Property(e => e.Id).ForSqlServer().UseIdentity(); And you would configure a property for Sequences like this: ForSqlServer().UseSequence(); The urls have changed due to the aspnet-core reorg, and the methods have

How could I Mock the FromSql() method?

心不动则不痛 提交于 2019-12-01 02:07:19
问题 I was wondering is there any way other than building a wrapper for mocking the FromSql ? I know this method is static, but since they added things like AddEntityFrameworkInMemoryDatabase to entity framework core, I thought there might be a solution for this too, I use EF Core 1.0.1 in my project. My end goal is to test this method: public List<Models.ClosestLocation> Handle(ClosestLocationsQuery message) { return _context.ClosestLocations.FromSql( "EXEC GetClosestLocations {0}, {1}, {2}, {3}"

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_PostTag_Tag_TagId”

冷暖自知 提交于 2019-12-01 01:54:22
I am using Entity Framework 7 RC1 and I have the entities: public class Post { public Int32 Id { get; set; } public String Title { get; set; } public virtual IList<PostTag> PostsTags { get; set; } } public class Tag { public Int32 Id { get; set; } public String Name { get; set; } public virtual IList<PostTag> PostsTags { get; set; } } public class PostTag { public Int32 PostId { get; set; } public Int32 TagId { get; set; } public virtual Post Post { get; set; } public virtual Tag Tag { get; set; } } The model configuration for these entities is the following: protected override void

EF Core 2.0 Identity - Adding navigation properties

£可爱£侵袭症+ 提交于 2019-12-01 01:46:44
In EF Core 2.0 Identity navigation properties are not included by default, so after upgrading, I added them. So for Many-To-Many relationship between User and Role, and One-To-Many relationship between Role and RoleClaim, I added following navigation properties: public class User : IdentityUser { [Required] public string Name { get; set; } public virtual ICollection<IdentityUserRole<string>> Roles { get; set; } } public class Role : IdentityRole { [Required] public string Name { get; set; } public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set;} } Surprisingly it adds an

EF7 change connectionstring at runtime

耗尽温柔 提交于 2019-12-01 01:46:16
In the previous versions of EF we were able to alter the dbcontext connection string as below : context.Database.Connection.ConnectionString = "the new connectionstring"; How can we do this with EF7? Thank you I found the solution : https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code Context Code public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } public DbSet<Blog> Blogs { get; set; } } Application code var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(@