Место общения программистов C#
Anonymous
Как предотвратить генерирование свойства тени для одного ко многим отношениям, когда может быть использован более одного
Сообщение
Anonymous » 30 июн 2025, 20:01
У меня есть этот класс модели: < /p>
Код: Выделить всё
internal abstract class GraphNodeModel
{
public Guid Id { get; }
public Guid TypeId { get; }
public virtual GraphNodeTypeModel? Type { get; }
public List Attributes { get; init; } = [];
public List Relations { get; init; } = [];
protected GraphNodeModel(Guid typeId)
{
TypeId = typeId;
}
public class Configurator : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder
.ToTable("graph_nodes")
.UseTptMappingStrategy();
builder.Property(n => n.Id)
.HasColumnName("graph_node_id")
.HasValueGenerator();
builder.Property(n => n.TypeId)
.HasColumnName("graph_node_type_id");
builder.HasOne(n => n.Type)
.WithMany()
.HasForeignKey(n => n.TypeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(n => n.Attributes)
.WithOne()
.HasForeignKey(a => a.GraphNodeId);
}
}
}
< /code>
и эти отношения: < /p>
internal class GraphRelationModel
{
public Guid Id { get; init; }
public Guid NodeAId { get; init; }
public virtual GraphNodeModel? NodeA { get; init; }
public Guid NodeBId { get; init; }
public virtual GraphNodeModel? NodeB { get; init; }
public Guid RelationTypeId { get; init; }
public virtual RelationTypeModel? RelationType { get; init; }
public Dictionary Attributes { get; } = [];
public class Configurator : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder
.ToTable("graph_relations");
builder.HasOne(r => r.NodeA)
.WithMany();
builder.HasOne(r => r.NodeB)
.WithMany();
builder.Property(r => r.Id)
.HasColumnName("graph_relation_id")
.HasValueGenerator();
builder.Property(r => r.NodeAId)
.HasColumnName("graph_node_A_id");
builder.Property(r => r.NodeBId)
.HasColumnName("graph_node_B_id");
builder.Property(r => r.RelationTypeId)
.HasColumnName("relation_type_id");
builder.Property(r => r.Attributes)
.HasConversion(
dictionary => JsonSerializer.SerializeToDocument(dictionary, JsonSerializerOptions.Default),
jsonDocument => jsonDocument.Deserialize(JsonSerializerOptions.Default) ?? new());
}
}
}
< /code>
Идея здесь состоит в том, чтобы иметь отношения между узлами. Отношение отображения определено в GraphreLationModel , потому что иностранная ключ не может быть указан из GraphNodemodel . />
Код: Выделить всё
b.Property("GraphNodeModelId")
.HasColumnType("uuid")
.HasColumnName("graph_node_model_id");
b.HasIndex("GraphNodeModelId")
.HasDatabaseName("ix_graph_relations_graph_node_model_id");
Как предотвратить генерацию свойств тени? Могу ли я как -то настроить это свойство?
Подробнее здесь:
https://stackoverflow.com/questions/796 ... n-there-ca
1751302907
Anonymous
У меня есть этот класс модели: < /p> [code]internal abstract class GraphNodeModel { public Guid Id { get; } public Guid TypeId { get; } public virtual GraphNodeTypeModel? Type { get; } public List Attributes { get; init; } = []; public List Relations { get; init; } = []; protected GraphNodeModel(Guid typeId) { TypeId = typeId; } public class Configurator : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder .ToTable("graph_nodes") .UseTptMappingStrategy(); builder.Property(n => n.Id) .HasColumnName("graph_node_id") .HasValueGenerator(); builder.Property(n => n.TypeId) .HasColumnName("graph_node_type_id"); builder.HasOne(n => n.Type) .WithMany() .HasForeignKey(n => n.TypeId) .OnDelete(DeleteBehavior.Restrict); builder.HasMany(n => n.Attributes) .WithOne() .HasForeignKey(a => a.GraphNodeId); } } } < /code> и эти отношения: < /p> internal class GraphRelationModel { public Guid Id { get; init; } public Guid NodeAId { get; init; } public virtual GraphNodeModel? NodeA { get; init; } public Guid NodeBId { get; init; } public virtual GraphNodeModel? NodeB { get; init; } public Guid RelationTypeId { get; init; } public virtual RelationTypeModel? RelationType { get; init; } public Dictionary Attributes { get; } = []; public class Configurator : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder .ToTable("graph_relations"); builder.HasOne(r => r.NodeA) .WithMany(); builder.HasOne(r => r.NodeB) .WithMany(); builder.Property(r => r.Id) .HasColumnName("graph_relation_id") .HasValueGenerator(); builder.Property(r => r.NodeAId) .HasColumnName("graph_node_A_id"); builder.Property(r => r.NodeBId) .HasColumnName("graph_node_B_id"); builder.Property(r => r.RelationTypeId) .HasColumnName("relation_type_id"); builder.Property(r => r.Attributes) .HasConversion( dictionary => JsonSerializer.SerializeToDocument(dictionary, JsonSerializerOptions.Default), jsonDocument => jsonDocument.Deserialize(JsonSerializerOptions.Default) ?? new()); } } } < /code> Идея здесь состоит в том, чтобы иметь отношения между узлами. Отношение отображения определено в GraphreLationModel [/code], потому что иностранная ключ не может быть указан из GraphNodemodel . /> [code]b.Property("GraphNodeModelId") .HasColumnType("uuid") .HasColumnName("graph_node_model_id"); b.HasIndex("GraphNodeModelId") .HasDatabaseName("ix_graph_relations_graph_node_model_id"); [/code] Как предотвратить генерацию свойств тени? Могу ли я как -то настроить это свойство? Подробнее здесь: [url]https://stackoverflow.com/questions/79684411/how-to-prevent-generating-shadow-property-for-one-to-many-relation-when-there-ca[/url]