Model
```C#
public class Blog
{
public int Id { get; set; }
public Blog MyBlog { get; set; }
public Blog InverseBlog { get; set; }
}
// Write following in OnModelCreating method
modelBuilder.Entity
```
Watch it 馃敟
Throw in this line in OnModelCreating it ignores the relationship and creates database without FK.
modelBuilder.Entity<Blog>().Property(e => e.Id).HasColumnName("id");
This worked in 1.1.2 packages creating a self-ref fk. (defining such relationship is possible in SqlServer).
It initially gave error on cascade but setting behavior to restrict worked.
CREATE TABLE [Blogs] (
[Id] int NOT NULL,
CONSTRAINT [PK_Blogs] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Blogs_Blogs_Id] FOREIGN KEY ([Id]) REFERENCES [Blogs] ([Id]) ON DELETE NO ACTION
);
Impact: Self-referencing PK-to-PK relationships cause stack overflow.
Risk: Low, the fix only affects this scenario
This is a big issue for us. Is there any likely release date for 2.0.1? Thanks
This is also a blocker for us, interested to know the timescales for 2.0.1 too...
Should also note that this also appears to affect data annotated models..
Tentative schedule for this fix is early October, but that could change.
Waiting a solution too. Tks.
That had to fail a unit test somewhere!
I think the tests missed this because a PK-to-PK self referencing relationship doesn't seem to have any point. It always points to itself and can never change. For people hitting this, it would be interesting to know if there is a reason that your databases contain these relationships?
I don't even have anything that crazy and I'm getting the same error... In my example a person should have a TimeZone associated with them. Here are two tables I have in the DB.
CREATE TABLE [dbo].[TimeZone]
(
[TimeZoneId] SMALLINT NOT NULL PRIMARY KEY IDENTITY,
[Code] VARCHAR(5) NOT NULL,
[Name] VARCHAR(150) NOT NULL,
[Description] VARCHAR(150) NOT NULL
)
CREATE TABLE [dbo].[Person] (
[Id] INT NOT NULL,
[FirstName] VARCHAR (50) NULL,
[MiddleName] VARCHAR (50) NULL,
[LastName] VARCHAR (50) NULL,
[PersonPrefixId] SMALLINT NULL,
[Suffix] VARCHAR (20) NULL,
[Title] VARCHAR (50) NULL,
[TimeZoneId] SMALLINT NOT NULL DEFAULT 13,
[AppUserId] UNIQUEIDENTIFIER NULL,
[CreatedByUserId] UNIQUEIDENTIFIER NOT NULL,
[CreatedDate] DATETIME CONSTRAINT [DF_Person_CreatedDate] DEFAULT (getutcdate()) NOT NULL,
[LastModifiedByUserId] UNIQUEIDENTIFIER NOT NULL,
[LastModifiedDate] DATETIME CONSTRAINT [DF_Person_LastModifiedDate] DEFAULT (getutcdate()) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Person_is_a_Party] FOREIGN KEY ([Id]) REFERENCES [dbo].[Party] ([Id]),
CONSTRAINT [FK_Person_PersonPrefixId] FOREIGN KEY ([PersonPrefixId]) REFERENCES [dbo].[PersonPrefix] ([Id]),
CONSTRAINT [FK_Person_CreatedByUserId] FOREIGN KEY ([CreatedByUserId]) REFERENCES [dbo].[AppUser]([Id]),
CONSTRAINT [FK_Person_LastModifiedByUserId] FOREIGN KEY ([LastModifiedByUserId]) REFERENCES [dbo].[AppUser]([Id]),
CONSTRAINT [FK_Person_AppUserId] FOREIGN KEY ([AppUserId]) REFERENCES [dbo].[AppUser]([Id]),
CONSTRAINT [FK_Person_TimeZoneId] FOREIGN KEY ([TimeZoneId]) REFERENCES [dbo].[TimeZone] ([TimeZoneId]),
)
After the database is created from the project.. I use the following scaffolding command to generate my classes.
Scaffold-DbContext -Verbose "Server=BLAH;Database=OC;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Schemas "dbo" -Force
Here are the classes that are generated by the scaffolding:
public partial class Person
{
public TimeZone TimeZone { get; set; }
}
public partial class TimeZone
{
public TimeZone()
{
Person = new HashSet<Person>();
}
public short TimeZoneId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<Person> Person { get; set; }
}
Then, with a simple console application running it breaks on accessing the data....
var _ctx = serviceProvider.GetService
var tzList = _ctx.TimeZone.ToList();
Exception : System.StackOverflowException
Message Evaluation of method System.Exception.get_Message requires calling method System.Reflection.RuntimeMethodInfo.CreateDelegate, which cannot be called in this context.
In the EF file Annotatable.cs Line 130
Annotation annotation;
return _annotations.Value.TryGetValue(name, out annotation)
? annotation
: null;
Let me know, I'm sure I can put together a smaller example project if you need me to.
@AndriySvyryd Can you verify the code above is the same root cause and is fixed in 2.0.1?
@dabasejumper Could you share a repro with a complete model? The original DB schema is not needed.
Hi @AlanMacdonald, @Nyami, @dabasejumper, @tandradeflorencio. We are gathering information on the use of EF Core pre-release builds. You reported this issue shortly after the release of 2.0.0 RTM. It would be really helpful if you could let us know:
Thanks in advance for any feedback. Hopefully this will help us to increase the value of pre-release builds going forward.
__Workaround:__ If you are scaffolding from an existing database you can use the query from https://github.com/aspnet/EntityFrameworkCore/issues/9462#issuecomment-325388454 to find the redundant FKs that cause this issue. Then either drop them or comment out the corresponding navigation properties.
@AndriySvyryd @ajcvickers I could not reproduce this issue with a simple two table solution. I tried last night. It wasn't even an issue with the EF/Linq query that I was trying to execute. It was further down the model.
So sql from #9462 (comment) fixed my issue. It was NOT with the two tables I was looking it. It unveiled a typo that I had made in one of the table designers of in my DB project.
It was as described above... A self referencing FK.... Was supposed to be DataLoadBatch instead of just DataLoad :)
Thank you for your feedback helping me thru this issue... Although, this issue probably needs a better error message when it is encountered.
Thanks for everything... Mike
This patch bug is approved for the 2.0.x patch. Please send a PR to the feature/2.0.1 branch and get it reviewed and merged. When we have the rel/2.0.1 branches ready please port the commit to that branch.
Hi, we have a public test feed that you can use to try out the ASP.NET/EF Core 2.0.3 patch!
To try out the pre-release patch, please refer to the following guide:
We are looking for feedback on this patch. We'd like to know if you have any issues with this patch by updating your apps and libraries to the latest packages and seeing if it fixes the issues you've had, or if it introduces any new issues. If you have any issues or questions, please reply on this issue to let us know as soon as possible.
Thanks,
Eilon
Most helpful comment
__Workaround:__ If you are scaffolding from an existing database you can use the query from https://github.com/aspnet/EntityFrameworkCore/issues/9462#issuecomment-325388454 to find the redundant FKs that cause this issue. Then either drop them or comment out the corresponding navigation properties.