ef-fluent-api

Specify only one index with fluent api

跟風遠走 提交于 2019-12-11 18:09:33
问题 Is there a way to only specify one index when overriding the OnModelCreating function in our context folder? I added: builder.Entity<ApplicationUser>() .HasIndex(e => e.Id); Which does add that as an index, but it looks like a few other properties have been made an index as well. Is there a way to make it the only index? The reason why I want to specify one index is: my migration file theres a bunch of dropForeignKeys, RenameTable, CreateIndex for things that i haven't touched. It happens to

How to configure one to zero and one to one relationship in fluent api with different PK and FK

落花浮王杯 提交于 2019-12-11 07:03:13
问题 I am a beginner and learning fluent API from tutorials. I read the some of the already given SO solution about 1-0, 1-* but I could not understand them properly. I simply want to use fluent API to set up One to One and One to Zero Relationship provided no convention followed. House and Room (terrible example) Requirement: 1. One House can have zero room or ONE room MAX. 2. One Room must be inside a house. 3. If a room is deleted House SHOULD NOT Get Deleted 4. If a House is getting deleted

How do I enforce an either-or relationship between model properties, using EF Core?

非 Y 不嫁゛ 提交于 2019-12-11 06:25:45
问题 I'm using EF Core. My Customer entity has properties Address1 , Address2 , and AddressFull . Depending on which system sends me the data, I may receive Address1 and Address2 , or I may receive AddressFull . So I need: EITHER Address1 and Address2 required, and AddressFull not-required OR Address1 and Address2 not-required, and AddressFull required So I have: entityTypeBuilder.Property(p => p.Address1).IsRequired(false); entityTypeBuilder.Property(p => p.Address2).IsRequired(false);

Entity framework - Define connection where neither side is required

好久不见. 提交于 2019-12-11 06:06:19
问题 I have a problem, I have to create a model where we have two entities which CAN be linked together but can also exist as stand alone entities. The model currently looks like this: public class Authorisation { public int AuthorisationID { get; set; } public virtual Change Change { get; set; } } public class Change { public int ChangeID{ get; set; } public virtual Authorisation Authorisation { get; set; } public int? AuthorisationID{ get; set; } } The reason for this is that we can have an

Entity Framework 6 DbUpdateConcurrencyException When Adding New Rows To Database

隐身守侯 提交于 2019-12-11 03:45:13
问题 Here's my problem: Every time I attempt to add a row to the database, I get this exception: DbUpdateConcurrencyException No one else uses this development database except me. Please help. I'm at my wits end here. Here's the code: using (var context = new MyDbContext(connectionString)) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); var newProduct = new ProductsEntity { ProductTypeId = 1 }; context.Products.Add(newProduct); // The line below always throws the exception

EF6 preventing not to create Index on Foreign Key

此生再无相见时 提交于 2019-12-10 19:15:26
问题 I'm using EF6 code first approach to create database. When i add migration and update database it always create Non-cluster Index for every foreign key in the table by default. My Question: Is there any global setting for EF6 to not create Non-Cluster index on foreign key ? I have search and found the following solutions Solution 1: Remove index line from migration before updating database Solution 1 not suits me because i have a lot of tables and my db is already created. Manually remove

How to retrieve Entity Configuration from Fluent Api

人走茶凉 提交于 2019-12-10 02:00:27
问题 Using Entity-Framework 6 I'm able to set up the configuration through Fluent Api like this: public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser> { public ApplicationUserConfiguration() { this.HasKey(d => d.Id); this.Ignore(d => d.UserId); } } Source from this question Using the attribute approach I'm able to know what's the property roles by reflection, but I wonder how can I retrieve these configurations, like Key for example, with Fluent Api approach? There's

Mapping to already existing database table

ぃ、小莉子 提交于 2019-12-09 19:14:37
问题 I am using Entity Framework 4.1 code first to connect to an already existing database. The table that I am using first is called Bank . I also have a Bank class as my domain model. This is how I mapped my class and table: public class HbfContext : DbContext { public DbSet<Bank> Banks { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Bank>().ToTable("Bank"); } } My Bank table: BankID INT BankName VARCHAR(50) My Bank class looks like this:

Composite Key EF Core getting error when using Fluent Api

不羁的心 提交于 2019-12-08 21:53:25
问题 So I have the following class in Entity Framework Core. I am trying to do a code first migration and can't for the life of me figure out how to make the fluent API for this work. public class Participants { public Activity Activity { get; set; } //Class with Id and Name of Activity public ApplicationUser Participant { get; set; } [Key] [Column(Order = 1)] public int ActivityId { get; set; } [Key] [Column(Order = 2)] public string ParticipantId { get; set; } } In EF6 I was able to do this in

HasOne not found in EF 6

杀马特。学长 韩版系。学妹 提交于 2019-12-08 19:37:49
问题 I am very new to Entity Framework and I am trying to figure out relations. I have found this code: class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag>()