Strawby
Strawby
CC#
Created by Strawby on 8/23/2023 in #help
❔ ✅ `System.Runtime` Assembly missing in `CSharpCompilation` when using referenced Attribute
Heya! GuraWave2 I've been playing around with testing source generators and I ran into an odd issue when getting the SymbolInfo of an Attribute that is part of a referenced assembly. I have both the assembly and the Syste.Runtime assembly referenced in the CSharpCompilation which the generator driver is using. What's the issue? When calling <SemanticModel>.GetSymbolInfo(<AttributeSyntax>).Symbol I would expect it to return the attribute symbol for the attribute from the referenced assembly. Instead, I get null. I have a minimal reproducible example here: https://github.com/StrawbrryFlurry/CSharpCompilationAttributeReference, which shows this behavior. I've tried to investigate this a little further, following the GetSymbolInfo call leading me to this line: https://github.com/dotnet/roslyn/blob/72e0a39fe59cd242dbf441df1f8beae694c8f068/src/Compilers/CSharp/Portable/Compilation/PublicSemanticModel.cs#L20-L21 where attributeType is an ExtendedErrorTypeSymbol, that is able to resolve the namespace of the attribute in the referenced assembly (ReferenceLibrary.ReferencedAttribute) with the error: error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Would anyone happen to know how I can fix this? Am I not referencing the System.Runtime Assembly correctly? I'd greatly appreciate any pointing in the right direction Smile
36 replies
CC#
Created by Strawby on 1/15/2023 in #help
❔ Generic entity type in EF Core
Heya everyone! GuraWave2 the recommended way to work with entities that have a property with a generic type on them? e.g:
internal sealed class MetricsEntity<TMetric> : Entity
where TMetric : class
{
public TMetric Data { get; set; } = default!;
}
internal sealed class MetricsEntity<TMetric> : Entity
where TMetric : class
{
public TMetric Data { get; set; } = default!;
}
I'd like to serialize and deserialize the data as JSON with a config that looks something like this: (I don't need to query the JSON data in the db)
internal sealed class MetricEntityConfiguration<TMetric> : IEntityTypeConfiguration<MetricsEntity<TMetric>>
where TMetric : class
{
public void Configure(EntityTypeBuilder<MetricsEntity<TMetric>> builder)
{
builder.ToTable(MetricTableNames.Metric, MetricTableNames.SchemaName);
builder.HasKey(x => x.Id);
builder.OwnsOne(x => x.Data, b => b.ToJson());
}
}
internal sealed class MetricEntityConfiguration<TMetric> : IEntityTypeConfiguration<MetricsEntity<TMetric>>
where TMetric : class
{
public void Configure(EntityTypeBuilder<MetricsEntity<TMetric>> builder)
{
builder.ToTable(MetricTableNames.Metric, MetricTableNames.SchemaName);
builder.HasKey(x => x.Id);
builder.OwnsOne(x => x.Data, b => b.ToJson());
}
}
Sadly though, that doesn't seem to work as I get an error saying: The entity type 'MetricsEntity<TestMetricData>' was not found. (I do have the model builder configured to use all type builders of the assembly) What's the preferred way to implement this? confuseddog
- Do I dynamically add a type configuration for all constructed generic types using reflection? - Do I change the property to be a string and serialize / deserialize it myself? - Do I do something entirely different? :0
44 replies