Problem mit dem EF -Kern: "Der Entitätstyp erfordert, dass ein Primärschlüssel definiert wird."
Posted: 24 Feb 2025, 04:20
Ich arbeite mit EF Core und begegne ein Problem, wenn ich versuche, einen DBContext zu erstellen. In der Fehlermeldung heißt es: < /p>
Der Entitätstyp 'Cafeid' erfordert, dass ein Primärschlüssel definiert wird. Wenn Sie einen schlüssellosen Entitätstyp verwenden wollten, rufen Sie 'Hasnokey' in 'OnModelCreating' auf. /p> Datensatz
Entitätsbasisklasse
Klasse
als Datensatztyp, aber EF Core scheint es eher als Entität als als Wertobjekt zu behandeln) CAFEID auf GUID
[/list]
Gibt es einen empfohlenen Ansatz für die Verwendung einer stark getippten ID und sicherstellen EF Core erkennt es richtig?
Der Entitätstyp 'Cafeid' erfordert, dass ein Primärschlüssel definiert wird. Wenn Sie einen schlüssellosen Entitätstyp verwenden wollten, rufen Sie 'Hasnokey' in 'OnModelCreating' auf. /p>
Code: Select all
public class Cafe : Entity
{
private Cafe() : base(default!) { }
private Cafe(
CafeId cafeId,
string name,
string description,
string location) : base(cafeId)
{
CafeId = cafeId;
Name = name;
Description = description;
Location = location;
}
public CafeId CafeId { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public string Location { get; private set; }
public string? Logo { get; private set; }
public static Result Create(
string name,
string description,
string location,
string? logo = null)
{
if (string.IsNullOrEmpty(name))
{
return Result.Failure(DomainError.Cafe.EmptyName);
}
var cafe = new Cafe(
CafeId.GenerateID(),
name,
description,
location)
{
Logo = logo
};
return cafe;
}
}
< /code>
CafeId
Code: Select all
public record CafeId
{
private CafeId() { }
public CafeId(Guid value) => Value = value;
public static CafeId GenerateID() => new(Guid.NewGuid());
public Guid Value { get; }
public static Result Create(Guid value)
{
if (value == Guid.Empty)
{
return Result.Failure(Error.Null);
}
return new CafeId(value);
}
}
< /code>
Entity configuration:
public class DbSetCafe : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("cafe");
builder.HasKey(c => c.CafeId);
builder.Property(c => c.CafeId)
.HasConversion();
builder.Property(c => c.Name)
.HasMaxLength(100)
.IsRequired();
builder.Property(c => c.Description)
.HasMaxLength(500)
.IsRequired();
builder.Property(c => c.Location)
.HasMaxLength(200)
.IsRequired();
builder.Property(c => c.Logo)
.HasMaxLength(2000);
builder.HasMany()
.WithOne()
.HasForeignKey("CafeId")
.OnDelete(DeleteBehavior.Cascade);
}
}
< /code>
Converter
public class CafeIdConvertor : ValueConverter
{
public CafeIdConvertor() : base(IN => IN.Value,
OUT => new CafeId(OUT))
{
}
}
< /code>
Entity
Code: Select all
public abstract class Entity : IEquatable where TId : notnull
{
protected Entity(TId id) => Id = id;
public TId Id { get; private init; }
public static bool operator ==(Entity? first, Entity? second) => first is not null && second is not null && first.Equals(second);
public static bool operator !=(Entity? first, Entity second) => !(first == second);
public override bool Equals(object? obj)
{
if (obj == null)
{
return false;
}
if (obj.GetType() != GetType())
{
return false;
}
if (obj is not Entity entity)
{
return false;
}
return EqualityComparer.Default.Equals(entity.Id, Id);
}
public bool Equals(Entity? other)
{
if (other == null)
{
return false;
}
if (other.GetType() != GetType())
{
return false;
}
return EqualityComparer.Default.Equals(other.Id, Id);
}
public override int GetHashCode() => Id.GetHashCode();
}
< /code>
DBContext
Code: Select all
public class EmployeeDBContext : DbContext
{
public EmployeeDBContext(DbContextOptions options)
: base(options) { }
public DbSet Employee { get; set; }
public DbSet Cafe { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSnakeCaseNamingConvention();
optionsBuilder.EnableSensitiveDataLogging();
}
}
< /code>
[b]What I've tried[/b]
[list]
[*]Defined CafeID
Code: Select all
CafeIdConvertor
[/list]
Gibt es einen empfohlenen Ansatz für die Verwendung einer stark getippten ID und sicherstellen EF Core erkennt es richtig?