entity-framework-core

Auto increment non key value entity framework core 2.0

隐身守侯 提交于 2020-07-05 07:26:19
问题 I've got an object that has a key stored as a GUID as well as a friendlyID, like this: public class Job { [Key] public Guid Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int FriendlyId { get; set; } public string Description { get; set; } } However when I try to update description using my update method: public void Update(Job job, Job jobParam) { if (job == null) { throw new AppException("Job does not exist"); } //Update job properties job.Description =

Auto increment non key value entity framework core 2.0

二次信任 提交于 2020-07-05 07:25:24
问题 I've got an object that has a key stored as a GUID as well as a friendlyID, like this: public class Job { [Key] public Guid Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int FriendlyId { get; set; } public string Description { get; set; } } However when I try to update description using my update method: public void Update(Job job, Job jobParam) { if (job == null) { throw new AppException("Job does not exist"); } //Update job properties job.Description =

Ignore properties in data model while keeping them in EF Core migrations

徘徊边缘 提交于 2020-07-04 20:50:24
问题 I am creating a greenfield application that uses EF Core which must talk to a legacy database. I want EF to ignore some of the columns in the database because they will eventually be deprecated and I don't want them in the new entity model. I can't remove them yet as the legacy system still relies on them. For an unwanted database column called DeprecatedFeature , I want to do something like: modelBuilder.Entity<MyEntity>(entity => { entity.HasKey(t => t.Id); entity.ToTable("MyEntity");

How to persist a list of strings with Entity Framework Core?

烂漫一生 提交于 2020-07-04 07:09:01
问题 Let us suppose that we have one class which looks like the following: public class Entity { public IList<string> SomeListOfValues { get; set; } // Other code } Now, suppose we want to persist this using EF Core Code First and that we are using a RDMBS like SQL Server. One possible approach is obviously to create a wraper class Wraper which wraps the string: public class Wraper { public int Id { get; set; } public string Value { get; set; } } And to refactor the class so that it now depends on

Entity Framework Core cascade delete one to many relationship

老子叫甜甜 提交于 2020-07-01 10:38:05
问题 public class Station : IEntitie { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDispatchStations { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDestinationStations { get; set; } } public class RegulatorySchedule : IEntitie { [Key] public int Id { get; set; } public virtual Station DispatchStation { get; set; } public virtual Station DestinationStation {

Entity Framework Core cascade delete one to many relationship

懵懂的女人 提交于 2020-07-01 10:37:48
问题 public class Station : IEntitie { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDispatchStations { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDestinationStations { get; set; } } public class RegulatorySchedule : IEntitie { [Key] public int Id { get; set; } public virtual Station DispatchStation { get; set; } public virtual Station DestinationStation {

One-to-one and one-to-many relationships with same owned entity type

≡放荡痞女 提交于 2020-06-29 04:14:31
问题 After this question I tried working with owned entity types but I'm stuck at the following scenario: public class User { [Key] public Guid UserId { get; set; } public ICollection<Listing> Listings { get; set; } public Image Avatar { get; set; } } public class Listing { [Key] public Guid ListingId { get; set; } public ICollection<Image> Photos { get; set; } } [Owned] public class Image { public long Size { get; set; } public string Height { get; set; } public string Width { get; set; } }

EF Core - Cannot insert explicit value for identity column SqlException

北战南征 提交于 2020-06-29 01:11:34
问题 In my app I am using EF Core to define 2 entities (Code First), Meeting and PostcodeDetail defined as follows: public class Meeting { public int Id { get; set;} public PostcodeDetail PostcodeDetail { get; set; } // other properties removed for brevity } public class PostcodeDetail { public int Id { get; set; } public ICollection<Meeting> Meetings { get; set; } = new List<Meeting>(); // other properties removed for brevity } When I create a new Meeting and try assigning an existing

EF Core - Cannot insert explicit value for identity column SqlException

一曲冷凌霜 提交于 2020-06-29 01:01:55
问题 In my app I am using EF Core to define 2 entities (Code First), Meeting and PostcodeDetail defined as follows: public class Meeting { public int Id { get; set;} public PostcodeDetail PostcodeDetail { get; set; } // other properties removed for brevity } public class PostcodeDetail { public int Id { get; set; } public ICollection<Meeting> Meetings { get; set; } = new List<Meeting>(); // other properties removed for brevity } When I create a new Meeting and try assigning an existing

IsNumeric in EF Core

拟墨画扇 提交于 2020-06-28 06:46:29
问题 Is there an equivalent of IsNumeric in EF Core or by using linq or Dynamic Sql or similar? I am trying to get only the rows with numeric values from a nvarchar column. 回答1: You could use Raw SQL Queries Security Note: Always use parameterization for raw SQL queries Given this dataset/result: The entity class for EF Core: Sandbox.cs public class Sandbox { public int SandboxId { get; set; } public string StringOrNumber { get; set; } } Create a DbSet in your context: public DbSet<Sandbox>