entity-framework-core

Insert entity with many to many related entities which exist in the database

做~自己de王妃 提交于 2019-12-08 11:54:20
问题 I have two objects with many to many relationship: public class Order { public int OrderID { get; set; } public string OrderName { get; set; } public virtual Collection<Product> Products { get; set; } = new Collection<Product>(); } public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public int OrderID { get; set; } public virtual Collection<Order> Orders { get; set; } = new Collection<Order>(); } // Mapping table public class OrderProduct { public

Insert entity with a collection of related entities which already exist in the database

醉酒当歌 提交于 2019-12-08 11:10:48
问题 I have two objects with a many-to-one relationship: public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public virtual Collection<ProductInventory> ProductInventorys { get; set; } = new Collection<ProductInventory>(); } public class ProductInventory { public int ProductInventoryID { get; set; } public string ProductInventoryName { get; set; } public int ProductID { get; set; } public virtual Product ProductFK { get; set; } } I would like to add a

Why is Entity Framework navigation property null?

时间秒杀一切 提交于 2019-12-08 11:09:10
问题 I am use code first model with a relationship below public class ApplicationUser : IdentityUser { public string UserFirstName { get; set; } public string UserLastName { get; set; } public string UserSchool { get; set; } public UserProfileData UserProfileData { get; set; } public int? MedicalSpecialtyId { get; set; } public virtual MedicalSpecialty MedicalSpecialty { get; set; } // public int? AnalyticsDataId { get; set; } // public ICollection<AnalyticsData> AnalyticsDatas { get; set; } }

Entity Framework 7: get database time

旧巷老猫 提交于 2019-12-08 09:45:18
问题 What is the best method to get the database server time in EF7? I discovered so far that is currently no support for query with output parameters. 回答1: If you only want to use Entity Framework, you can do it like that: Having this context: // My Dbcontext public class EF7Context : DbContext { } And the utility function: public static DateTime GetDbDateTime() { using (var context = new EF7Context()) { using (var connection = context.Database.GetDbConnection()) { if (connection.State ==

One-to-many relationship getting error in Entity Framework Core when trying to insert data

大城市里の小女人 提交于 2019-12-08 07:16:51
问题 I'm trying to add data to one-to-many relationship tables. But there is an exception. There are two tables: Post Attachment The Post has many Attachment but one Attachment has one unique post. I'm going to try: Adding records to the Post table, then after that using that post-Id. update the Attachment table Here is the exception thrown InvalidOperationException: The property 'Id' on entity type 'Posts' has a temporary value. Either set a permanent value explicitly or ensure that the database

EF context not responding in docker

微笑、不失礼 提交于 2019-12-08 07:00:32
问题 I just upgraded my Web API application to .NET core 3.0, all works fine when running in debug mode in IIS Express, but context does not respond when running in docker container either on server or in VS debug. No error thrown, just never responds. I tried deploying an updated image to server which is where I first noticed the issue. I then tried running as docker in vs to debug. I have updated all NuGet packages and set frameworks to .NET Core 3.0 or .NET Standard 2.1. I have inspected the

Efcore Spatial Query: A number is expected at position 9 of the input. The input has @p0. -

与世无争的帅哥 提交于 2019-12-08 06:47:56
问题 I'm trying to get around the fact that Entity Framework Core doesn't have support for Spatial Types by using the DBSet.FromSQL Method, and hand rolling a migration to add a geography column on SQL Server. Here's my DataContext public interface IDataContext { DbSet<PointOfInterest> PointsOfInterest { get; set; } int SaveChanges(); Task<int> SaveChangesAsync(CancellationToken cancellationToken); } public class DataContext : DbContext, IDataContext { public DataContext(DbContextOptions options)

Implementing Cascade Delete in a self referencing table in EF Core 2

柔情痞子 提交于 2019-12-08 05:39:45
问题 How can I implement Cascade Delete in a self referencing table in EF Core 2 (code first)? (For example there is a Comment Table and man can reply to a comment and this reply can reply by another.) public class Comment { public virtual int Id { get; set; } public virtual int? ParentId { get; set; } public Comment Parent { get; set; } public virtual IList<Comment> Replies { get; set; } public virtual string Description { get; set; } public virtual Article Article { get; set; } } 回答1: The

.NET Core database model changes in production

与世无争的帅哥 提交于 2019-12-08 05:11:36
问题 I follow code first approach to generate my databases by EFCore. Now during development one of my models changed. How can I update the corresponding database tables on a live system at one of our customers? I do not want to dump or delete any data of course. I'm still on .NET Core 1.1.0. 回答1: I fear you'll have to swallow the pill then. Database.EnsureCreated() is only useful for developing, where you don't have to preserve data. If you have to preserve data or change the table schema, you

How is the difference between add or delete an entity from the dbSet and from local property?

耗尽温柔 提交于 2019-12-08 05:01:14
问题 When I want to delete a property, I have two options: myDbContext.MyType.Add(myEntity); myDbContext.MyType.Local.Add(myEntity); In both cases, at least in my case, don't see differences, but I don't know really if it has a different behavior or not. Is it really the same or not? Thanks. 回答1: Based on documentation of methods: myDbContext.MyType.Add(myEntity); /// Begins tracking the given entity, and any other reachable entities that are /// not already being tracked, in the <see cref=