C
C#4mo ago
engineertdog

FK on Guid

I have the following two classes
c#
public class PiCollectiveIdentity {
public Guid Id { get; set; } = new Guid();
public long IdentityId { get; set; }
public required string Name { get; set; }
[ForeignKey("IdentityName")]
public IList<PiCollectiveIdentityMapping> Mappings { get; set; } = new List<PiCollectiveIdentityMapping>();
}

public class PiCollectiveIdentityMapping{
public Guid Id { get; set; } = new Guid();
public DateTime CollectionDate { get; set; } = DateTime.UtcNow;
public required string IdentityName { get; set; }
public PiCollectiveIdentity ? Identity { get; set; } = null;
}
c#
public class PiCollectiveIdentity {
public Guid Id { get; set; } = new Guid();
public long IdentityId { get; set; }
public required string Name { get; set; }
[ForeignKey("IdentityName")]
public IList<PiCollectiveIdentityMapping> Mappings { get; set; } = new List<PiCollectiveIdentityMapping>();
}

public class PiCollectiveIdentityMapping{
public Guid Id { get; set; } = new Guid();
public DateTime CollectionDate { get; set; } = DateTime.UtcNow;
public required string IdentityName { get; set; }
public PiCollectiveIdentity ? Identity { get; set; } = null;
}
Where each database record will have it's own unique ID, but the relationship between the two records is based on name (due to pulling the data from a 3rd party system). However, this throws an error when attempting to migrate for 'PiCollectiveIdentityMapping.Mappings' with foreign key properties {'IdentityName' : string} cannot target the primary key {'Id' : Guid} because it is not compatible What is the proper way to build the relationship?
5 Replies
Angius
Angius4mo ago
You set the FK on the "one" side of "one to many" And, I believe, you will need to configure the name to be a key
engineertdog
engineertdog4mo ago
I moved it from attribute based to the onModelCreating. I like to keep them in the models but sometimes they're just dumb
Jimmacle
Jimmacle4mo ago
another option is to use IEntityTypeConfiguration classes, then you can keep entity configuration out of the dbcontext and still use the fluent API
Jimmacle
Jimmacle4mo ago
Creating and Configuring a Model - EF Core
Overview of creating and configuring a Entity Framework Core model via Fluent API, Data Annotations and conventions.
engineertdog
engineertdog4mo ago
Oh right, I forgot about that method. That's certainly an option and if I end up with more scenarios where the attribute route doesn't cover the needs, I'll look to bring that back in
Want results from more Discord servers?
Add your server
More Posts
✅ How to create migration correctly? What do I do wrong?Command in Package Manager Console: PM> `add-migration UpdateSomeModels -contex ReadModelDbContext` MVC App's Post request not reachingFirst off, yes I already asked this last friday but couldn't follow trough due to not having the sou✅ Failed to deploy MVC PojectHi everyone, I am trying to deploy a .net 7 mvc project on somee.com but I keep getting this error✅ Can my Equals(IEnumerable<T>? x, IEnumerable<T>? y) be made much simpler?You may have any tricks to simplify the following. ```csharp public bool Equals(IEnumerable<T>? x, IHow to handle different environments in a C# repo?I have a github repo that has 3 different environments (dev, preprod, prod), and the difference is mRider Info Diagnostic Action LocationI've created an analyzer that emits a diagnostic with the location of a field's type and this works Blazor Web App and auth from separate webApi with IdentityHi! Im working on a project where I have a standalone ASP.Net Core WebAPI-project where i use the Id✅ LoadLibrary works, but does not show in Debug>Windows>Modules tabI'm trying to load a DLL file into my WPF app, and it works since the returned address is valid, GetMenu Strip not showing upI added the menu strip using the designer and some items to it but when I compile I don't see the meBlazor Web App (.NET 8) - Logout functionality with IdentityCoreWhat's the best way to implement log out functionality? Looks like I can't use `signInManager.SignOu