entity-framework-core

FromSql with Non-Existing Entity

两盒软妹~` 提交于 2020-07-22 21:38:21
问题 I have a very large, existing database with numerous views and tables. I need to combine the results of some views and tables. I want this combined data to be available in my DbContext but there is no single corresponding view or table that I can use to map the entity to the SQL results I want. To this end, I have set up a DbQuery in my context and I am using the OnModelCreating method to set up the query results, an example is below. Here are the entity object models. In this example Author

How to prevent a column update in EF Core 3.1?

老子叫甜甜 提交于 2020-07-22 03:55:49
问题 I upgraded from .Net Core 2.2 to 3.1 and this functionality has been deprecated modelBuilder .Entity<Order>() .Property(e => e.CreationTime) .ValueGeneratedOnAddOrUpdate() .Metadata.IsStoreGeneratedAlways = true; I need EF to do the Insert but block the update. Thanks! 回答1: According to the obsoleted property implementation: public virtual bool IsStoreGeneratedAlways { get => AfterSaveBehavior == PropertySaveBehavior.Ignore || BeforeSaveBehavior == PropertySaveBehavior.Ignore; set { if (value

How to prevent a column update in EF Core 3.1?

流过昼夜 提交于 2020-07-22 03:55:29
问题 I upgraded from .Net Core 2.2 to 3.1 and this functionality has been deprecated modelBuilder .Entity<Order>() .Property(e => e.CreationTime) .ValueGeneratedOnAddOrUpdate() .Metadata.IsStoreGeneratedAlways = true; I need EF to do the Insert but block the update. Thanks! 回答1: According to the obsoleted property implementation: public virtual bool IsStoreGeneratedAlways { get => AfterSaveBehavior == PropertySaveBehavior.Ignore || BeforeSaveBehavior == PropertySaveBehavior.Ignore; set { if (value

.net core injecting and resolving services

落花浮王杯 提交于 2020-07-20 17:06:58
问题 I am trying my hand at .net core web api. I have created a static method for registering my controllers like this: public static class RegistrationExtensions { public static void RegisterApplicationServices(this IServiceCollection services, IServiceProvider serviceProvider) { services.RegisterSingletons(); services.RegisterRequests(); services.RegisterTransient(serviceProvider); } public static void RegisterSingletons(this IServiceCollection services) { services.AddSingleton<Configuration>();

.net core injecting and resolving services

China☆狼群 提交于 2020-07-20 17:06:08
问题 I am trying my hand at .net core web api. I have created a static method for registering my controllers like this: public static class RegistrationExtensions { public static void RegisterApplicationServices(this IServiceCollection services, IServiceProvider serviceProvider) { services.RegisterSingletons(); services.RegisterRequests(); services.RegisterTransient(serviceProvider); } public static void RegisterSingletons(this IServiceCollection services) { services.AddSingleton<Configuration>();

Auto-Incremented value not working in PostgreSQL when using EntityFramework Core

三世轮回 提交于 2020-07-20 17:05:21
问题 It seems like the auto-increment function for PostgreSQL doesn't seem to work. I have the following code: namespace project.Models { public class DatabaseModel { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(Order=1, TypeName="integer")] public int ID { get; set; } } } When I update the database (after doing a migration) the ID column is the primary key without being a auto-increment. When I try to add a new object to the database I get the following error: Microsoft

Auto-Incremented value not working in PostgreSQL when using EntityFramework Core

元气小坏坏 提交于 2020-07-20 17:04:12
问题 It seems like the auto-increment function for PostgreSQL doesn't seem to work. I have the following code: namespace project.Models { public class DatabaseModel { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(Order=1, TypeName="integer")] public int ID { get; set; } } } When I update the database (after doing a migration) the ID column is the primary key without being a auto-increment. When I try to add a new object to the database I get the following error: Microsoft

PostgresException: 23505: duplicate key value violates unique constraint “PK_country”

点点圈 提交于 2020-07-18 20:58:20
问题 I am using EF Core 2.0 and Postgres 9.6. Whenever I insert data I get the error PostgresException: 23505: duplicate key value violates unique constraint "PK_country" By tracing it looks like EF doesnt generate AutoIncrement column My Model public partial class Country { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CountryId { get; set; } [Display(Name="Country Name")] public string CountryName { get; set; } } My Controller if (ModelState.IsValid) { _context.Add

PostgresException: 23505: duplicate key value violates unique constraint “PK_country”

烈酒焚心 提交于 2020-07-18 20:57:04
问题 I am using EF Core 2.0 and Postgres 9.6. Whenever I insert data I get the error PostgresException: 23505: duplicate key value violates unique constraint "PK_country" By tracing it looks like EF doesnt generate AutoIncrement column My Model public partial class Country { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CountryId { get; set; } [Display(Name="Country Name")] public string CountryName { get; set; } } My Controller if (ModelState.IsValid) { _context.Add

EF Core 3 x.Contains() in expression where x is ICollection

a 夏天 提交于 2020-07-18 11:03:56
问题 I've got the following data layer setup: public class Repository : IRepository { private readonly MyDbContext _dbContext; public List<Meter> Search(Expression<Func<Meter,bool>> criteria) IQueryable<Meter> results = _dbContext.Meters; return results.Where(criteria).ToList(); } } } ... from a client class: IRepository _repository; public void ClientMethod () { ICollection<int> ids = new List<int>() {1, 2, 3); var results = _repository.Search(c=> ids.Contains(c.Id)); // This throws exception }