Closed SetTrend closed 22 hours ago
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
I see … So here's the MRE.
Just run/debug through the sole test in the MSTest project (DatabaseBackEndTests.CreateDatabaseTests.SuccessfullyCreateDatabaseAndDelete()
).
Thanks for taking the time to investigate on this issue! 👍
@SetTrend It is your intention to map Language
as an entity type mapped to its own table?
Does it help if I provide the intended target database diagram?
Here is the T-SQL code I manually created for this diagram to appear (click Details to view the code):
The Language
entity is supposed to serve as filter and dropdown data source later in the future application.
Do you see any path to get this working soon?
Hello, @AndriySvyryd,
I'm using EF Core for a commercial project, and I need support. I'm an MS Enterprise customer. Do you see ANY way for me to go to get immediate support for this case?
@SetTrend you can contact Microsoft Support for EF like for other Microsoft products - see our support docs. Otherwise you'll have to be patient until we get around to investigating this - there are many things going on at the moment.
https://github.com/dotnet/efcore/issues/11336 and https://github.com/dotnet/efcore/issues/12078 would be needed to enable your scenario.
For now, something like this would be the closest:
modelBuilder.Entity<DataText>()
.HasOne(d => d.Language)
.WithOne()
.HasPrincipalKey<DataText>(d => d.LanguageId)
.HasForeignKey<Language>(l => l.Id);
[PrimaryKey(nameof(Id), nameof(LanguageId))]
public class DataText(string id, string languageId, string text)
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Length(2, 200)]
public string Id { get; init; } = id;
[Length(2, 2)]
[Column("Language")]
public string LanguageId { get; init; } = languageId;
public virtual Language Language { get; set; } = new Language(languageId);
[Length(2, 3000)]
public string Text { get; set; } = text;
}
Using Entity Framework Core, I created a
Language
entity with the 2-letter ISO code as key:Next, I created a dependant entity referencing that table:
Now, using the SQLite provider, I'd like to reference the
Language
entity from theDataText
entity using a foreign key. This doesn't seem possible. InOnModelCreating()
I'm getting the following error message:Same is true for SQL Server provider. So, I guess EF Core is not able to create foreign keys on strings, although a
CHAR(2)
foreign key is more efficient than anINT
foreign key. Adding an additionalINT
proxy primary key column would require to introduce an unnecessary burdon to the database.