-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to correctly reference other table using EF Core and CHAR(2) foreign key? #34543
Comments
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 ( Thanks for taking the time to investigate on this issue! 👍 |
@SetTrend It is your intention to map |
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): SET NOCOUNT ON
USE master
IF (SELECT COUNT(*) FROM sys.databases WHERE name = 'DbTest') = 1 DROP DATABASE DbTest
GO
CREATE DATABASE DbTest
ON PRIMARY
( NAME = 'DbTest'
)
GO
USE DbTest
CREATE TABLE Languages
( Id CHAR(2) PRIMARY KEY CHECK (LEN(LTRIM(RTRIM(Id))) = 2)
)
CREATE TABLE DataTexts
( Id VARCHAR(200)
, Language CHAR(2)
--
, Text NVARCHAR(3000) NOT NULL CHECK (LEN(LTRIM(RTRIM(Text))) > 1)
--
, CONSTRAINT PK_DataTexts PRIMARY KEY (Id, Language)
, CONSTRAINT FK_Language_Id FOREIGN KEY (Language) REFERENCES Languages (Id)
)
GO
INSERT INTO Languages VALUES ('de'), ('en')
INSERT INTO DataTexts VALUES
('ProfileItem.Label.1', 'de', N'Titel')
, ('ProfileItem.Label.1', 'en', N'Title')
, ('ProfileItem.Label.2', 'de', N'Zuletzt aktualisiert am {0:d}')
, ('ProfileItem.Label.2', 'en', N'Last modified on {0:d}')
, ('ProfileItem.Label.3', 'de', N'Verfügbar ab {0:d}')
, ('ProfileItem.Label.3', 'en', N'Available from {0:d}')
, ('ProfileItem.Label.4', 'de', N'Davon zu {0:P0} vor Ort verfügbar')
, ('ProfileItem.Label.4', 'en', N'With on-site availability up to {0:P0}') The |
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. |
#11336 and #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;
} |
@AndriySvyryd: I tried everything, but it doesn't seem to work:
I uploaded the updated code to branch |
For that you'd need #10000 As a workaround, set the navigation to dataTexts.Add(new DataText(textId, "de", itemLabels[i][0])
{
Language = null!
});
dataTexts.Add(new DataText(textId, "en", itemLabels[i][1])
{
Language = null!
}); Also, I made a mistake in the previous comment, the relationship should be one-to-many: modelBuilder.Entity<DataText>()
.HasOne(x => x.Language)
.WithMany()
.HasForeignKey(d => d.LanguageId)
.HasPrincipalKey(l => l.Id); |
It's working like a charm! Thanks so much, @AndriySvyryd, for your continued and prompt support. Allow me to give you (all) my grateful kudos for your excellent work an support in this repo. You are so excellently engaged in your project! Your are a solitary, shining lighthouse in the dark, forsaken world of Microsoft support. All the other repos don't give a damn:
It's so hard to work with Microsoft as their support (in the other repos) is so poor. As a professional I'd be willing to pay a subscription just to get immediate support and things to work. Thank you so much again. You have been lifting me from a heavy burdon this time. |
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.The text was updated successfully, but these errors were encountered: