EF core , Code First Table rename not detected for migration definition, Scafolder is empty - ef-code-first

I get empty migration builder when I change a class name in EF core.
In older EFs , it usually auto generate the code for renaming tables.
But not working in EF core
public class EventComment : Comment
{
[Key]
public int CommentID { get; set; }
public int? ParentID { get; set; }
public virtual EventComment Parent { get; set; }
public int EventID { get; set; }
public virtual EventMaster EventMaster { get; set; }
}
public class Comment
{
public string CommentTitle { get; set; }
public string CommentDetails { get; set; }
public int UpVoteCount { get; set; }
public int DownVoteCount { get; set; }
public int CommentEmotion { get; set; }
public string CommentedByID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
now changing EventComment to CommentMaster. The migration is empty.
I use fluent API for configuring
builder.Entity<EventComment>()
.HasOne(e => e.EventMaster)
.WithMany()
.HasForeignKey(m => m.EventID);
builder.Entity<EventComment>()
.HasOne(e => e.ApplicationUser)
.WithMany()
.HasForeignKey(m => m.CommentedByID);
builder.Entity<EventComment>()
.HasOne(e => e.Parent)
.WithMany()
.HasForeignKey(x => x.ParentID);

This is because of the EF Core default table mapping convention (highlights are mine):
By convention, each entity will be setup to map to a table with the same name as the DbSet<TEntity> property that exposes the entity on the derived context. If no DbSet<TEntity> is included for the given entity, the class name is used.
I guess this is different from the previous EF. The essential part is that although you renamed the entity class, if you keep the old DbSet property name, the table name will not change.

Related

Establishing one to one relations with Entity Framework 7

Having the following parent class:
[Table("smart_recharge_registro")]
public class SmartRechargeRegistro
{
[Key]
public int id { get; set; }
public SmartRechargeRequest request { get; set; }
public SmartRechargeProceso proceso { get; set; }
public SmartRechargeResponse response { get; set; }
}
Which in turn references the following child classes:
[Table("smart_recharge_request")]
public class SmartRechargeRequest
{
public String nombreDeUsuario { get; set; }
public String passwordDeUsuario { get; set; }
public String msisdnSuscriptor { get; set; }
}
and:
[Table("smart_recharge_proceso")]
public class SmartRechargeProceso
{
[Key]
public int id { get; set; }
public String carrierId { get; set; }
public String cliente { get; set; }
public String network { get; set; }
}
and lastly:
[Table("smart_recharge_response")]
public class SmartRechargeResponse
{
public Boolean responseSuccess { get; set; }
public int responseCode { get; set; }
public String? responseDetails { get; set; }
}
The Add-Migration and Update-Database command execute without problems. However, when I try to save
await _repository.RegistroColeccion.AddAsync(registro);
await _repositorio.SaveChangesAsync();
I get the following error:
Microsoft.EntityFrameworkCore.DbUpdateException: Could not save changes. Please configure your entity type accordingly.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Cannot add
or update a child row: a foreign key constraint fails
(beservicebroker_dev.registro_eventos_srdotnet, CONSTRAINT
FK_registro_eventos_srdotnet_SmartRechargeProceso_procesoid FOREIGN
KEY (procesoid) REFERENCES smartrechargeproceso (id) O)
To solve the problem, I tried to create one-to-one relationships following this tutorial
modelBuilder.Entity<SmartRechargeRegistro>()
.HasOne(s => s.request)
.WithOne(r => r.SmartRechargeRegistro)
.HasForeignKey<SmartRechargeRequest>(r => r.id);
modelBuilder.Entity<SmartRechargeRegistro>()
.HasOne(s => s.proceso)
.WithOne(p => p.SmartRechargeRegistro)
.HasForeignKey<SmartRechargeProceso>(p => p.id);
modelBuilder.Entity<SmartRechargeRegistro>()
.HasOne(s => s.response)
.WithOne(r => r.SmartRechargeRegistro)
.HasForeignKey<SmartRechargeResponse>(r => r.id);
Inside SmartRechargeRequest, SmartRechargeProceso and SmartRechargeResponse, added the following:
[JsonIgnore]
public SmartRechargeRegistro SmartRechargeRegistro { get; set; }
Also added inside SmartRechargeRequest and SmartRechargeResponse an id
[Key]
public int id { get; set; }
I'm still unable to test the endpoint because the SmartRechargeRequest and SmartRechargeResponse are completely disfigured in the swagger (even if the [JsonIgnore] or [IgnoreDataMember] annotations are set) due to the presence of that SmartRechargeRegistro object.
I'm pretty sure my solution is misguided and I'm getting the process completely wrong.
What would be the proper way to map one-to-one relationships for this case? Any help will be appreciated.
Please note that in reality, these classes are huge (dozens of properties), so it's not possible to merge all of them on a single table.

Entity Framework Core 3.1 Code-First Model - Define model of having multiple many to many relation with self

I like to create a DB model in ASP.Net Core 3.1. I am using Code First approach with EF Core 3.1.
I like to create a model for this relationship-
So, there is one Employee table and every employee has multiple bosses and each has multiple sub-ordinates. But every boss and every subordinate are employees also. What I have done is something like this-
Employee Model-
public class Employee
{
[HiddenInput(DisplayValue = false), Display(Name = "ID")]
[Key()]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 1)]
public Guid? Id { get; set; } = Guid.NewGuid();
[Column("Name"), Required(ErrorMessage = "Term Name should be given"), Display(Name = "Term Name", Prompt = "Please Give Term Name")]
[DataType(DataType.Text)]
public string Name { get; set; }
public ICollection<Boss> Bosses { get; set; }
public ICollection<Subordinate> Subordinates { get; set; }
............
............
}
But I am getting this error during creating the DB model by the command Add-Migration <MigrationName>-
Unable to determine the relationship represented by navigation property 'Employee.Bosses' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Can anyone please help?
direct many-to-many relations are not supported with ef core 3.1.
See this: https://learn.microsoft.com/de-de/ef/core/what-is-new/ef-core-5.0/whatsnew
If you cannot use ef core >= 5, than you have to create a navigation property to the EmployeeBoss entity.
Try this:
public partial class Employee
{
public Employee()
{
EmployeeBossEmployees = new HashSet<EmpoyeeBoss>();
EmployeeBossBosses = new HashSet<EmpoyeeBoss>();
}
[Key]
public int Id { get; set; }
[InverseProperty(nameof(EmpoyeeBoss.Employee))]
public virtual ICollection<EmpoyeeBoss> EmployeeBossEmployees { get; set; }
[InverseProperty(nameof(EmpoyeeBoss.Boss))]
public virtual ICollection<EmpoyeeBoss> EmployeeBossBosses { get; set; }
}
public partial class EmpoyeeBoss
{
[Key]
public int Id { get; set; }
public int BossId { get; set; }
public int EmployeeId { get; set; }
[ForeignKey(nameof(EmployeeId))]
[InverseProperty("EmployeeBossEmployees")]
public virtual Employee Employee { get; set; }
[ForeignKey(nameof(BossId))]
[InverseProperty("EmployeeBossBosses")]
public virtual Employee Boss { get; set; }
}
and include in your dbcontext:
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<EmpoyeeBoss> EmployeeBosses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmpoyeeBoss>(entity =>
{
entity.HasOne(d => d.Employee)
.WithMany(p => p.EmployeeBossEmployees)
.HasForeignKey(d => d.EmployeeId)
.OnDelete(DeleteBehavior.ClientSetNull);
entity.HasOne(d => d.Boss)
.WithMany(p => p.EmployeeBossBosses)
.HasForeignKey(d => d.BossId);
});

How to define two FK in the same table?

I have a table called User which inherit the properties from IdentityUser, inside that table I added a reference to the UserFriendship table which need to store all the user friendship:
public class User : IdentityUser
{
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public virtual ICollection<UserFriendship> UserFriendship { get; set; }
}
Essentially the UserFriendship contains two users, who are those who have a common friendship, this is the model definition:
public class UserFriendship
{
public int Id { get; set; }
[Key, ForeignKey("UserA")]
public string UserAId { get; set; }
public User UserA { get; set; }
[Key, ForeignKey("UserB")]
public string UserBId { get; set; }
public User UserB { get; set; }
[Required]
public DateTime DateTime { get; set; }
}
I defined the UserA and the UserB which are two FK of a User that are contained inside AspNetUsers table.
Now inside the FluentAPI I declared the following:
builder.Entity<UserFriendship>(entity =>
{
entity.HasKey(f => f.Id);
entity.HasOne(u => u.UserA)
.WithMany(n => n.UserFriendships)
.HasForeignKey(u => u.UserAId)
.IsRequired();
entity.HasOne(u => u.UserB)
.WithMany(n => n.UserFriendships)
.HasForeignKey(u => u.UserBId)
.IsRequired();
});
when I execute this command:
add-migration InitialMigration -context MyAppContext
I'll get:
Cannot create a relationship between 'User.UserFriendships' and 'UserFriendship.UserB', because there already is a relationship between 'User.UserFriendships' and 'UserFriendship.UserA'. Navigation properties can only participate in a single relationship.
I'm not an expert of EnityFramework, but based on that error I think that I cannot define two FK in the same table?
Sorry for any mistake, thanks.
You can define more than one FK in table.
The problem here is you are pointing two times to one navigation property - UserFriendships. The solution would be to create two navigation properties.
Navigation properties are used to browse the related data for specified foreign-key (you have one-to-many relationship) of entity.
Try this:
public class User
{
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<UserFriendship> UserAFriendships { get; set; }
public ICollection<UserFriendship> UserBFriendships { get; set; }
}
public class UserFriendship
{
public int Id { get; set; }
public string UserAId { get; set; }
public User UserA { get; set; }
public string UserBId { get; set; }
public User UserB { get; set; }
public DateTime DateTime { get; set; }
}
And define the relationship through fluent api as following:
modelBuilder.Entity<UserFriendship>(entity =>
{
entity.HasKey(f => f.Id);
entity.HasOne(u => u.UserA)
.WithMany(n => n.UserAFriendships)
.HasForeignKey(u => u.UserAId)
.IsRequired();
entity.HasOne(u => u.UserB)
.WithMany(n => n.UserBFriendships)
.HasForeignKey(u => u.UserBId)
.IsRequired();
});
What is more - you don't need to specify attributes Key, ForeignKey if you use Fluent API.

Two Foreign Keys in Entity Framework Core

I am using Code First approach while creating Database using Entity Framework Core. I would like to create two foreign keys pointing the same table.
My example shows User table which will hold userID and Message table which will hold both Receiver ID and Sender ID (what means both values have to point the same table).
Code for User:
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string UserName { get; set; }
[Required]
[MaxLength(50)]
public string Password { get; set; }
public virtual ICollection<Message> MessagesSent { get; set; }
public virtual ICollection<Message> MessagesReceived { get; set; }
}
For Message:
public class Message
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public User Sender { get; set; }
public User Receiver { get; set; }
public int senderId { get; set; }
public int receiverId { get; set; }
[MaxLength(500)]
public string message { get; set; }
}
I am using ASP.NET Core 2 and I am a newbie.
I was trying to use this solution, but unfortunately, I couldn't manage to override OnModelCreating method. It shows that it doesn't exist.
PS. don't mind password field, it is only for testing purpose.
Thank you!
I managed to make it works using Fluent API.
Code in my DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Message>()
.HasOne(p => p.Receiver)
.WithMany(t => t.MessagesReceived)
.HasForeignKey(m => m.ReceiverId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Message>()
.HasOne(p => p.Sender)
.WithMany(t => t.MessagesSent)
.HasForeignKey(m => m.SenderId)
.OnDelete(DeleteBehavior.Restrict);
}
What is more, I've discovered a problem with not set User deletion behaviour.
There are two options to solve it.
First is keeping Messages if User was deleted:
.OnDelete(DeleteBehavior.Restrict);
Or second which will remove Messages:
.OnDelete(DeleteBehavior.Cascade);

Code-first database mapping, table missing (wasn't created) for many-to-many relationship

I am building ASP.NET webforms application using Entity Framework 6.1, with code-first approach to generate database. I have two tables, Product and Tags, in many-to-many relationship. Classes are below:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
}
I want two junction tables out of this relationship ProductTags and ProductTagsTradeFor. So I overrided OnModelCreating of WebsiteDbContext.
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
After running the application, database was created and table ProductTradeForTags is present but table ProductTags was missing.
What is the problem and how do I fix it so both tables are created?
You can't share the navigation properties. You will need to add a second set of navigation collections to each:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> TradeForTags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
public virtual ICollection<Product> TradeForProducts{ get; set; }
}
Then
modelBuilder.Entity<Product>().HasMany(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany(s => s.TradeForTags).WithMany(c => c.TradeForProducts)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
Your model require to navigate from a Tag to Products and from a Product to Tags.
In this case one association table is enough.
EF should raise an exception but it simply ignores the first configuration.

Resources