问题
I have two Entity classes :
public class User
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[InverseProperty("User")]
public virtual UserDetail UserDetail { get; set; }
}
public class UserDetail
{
public string Biography { get; set; }
[Key]
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
And the DbContext is :
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<UserDetail> UserDetails { get; set; }
}
When I run command :
dotnet ef migrations add Initial
dotnet ef database update
The two tables will be generated . When I double click the "dbo.UserDetails" within the "SQL Server Object Explorer" of Visual Studio , there's a foreign key shown in Design View :
However , If right click the "dbo.UserDetails" , and choose "Script As" -> "Create To" -> "New Query Window" , the generated sql will be :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserDetails] (
[Biography] NVARCHAR (MAX) NULL,
[UserId] NVARCHAR (450) NOT NULL
);
What make me confused is that the FK CONSTRAINT is gone away here.
- Visual Studio : community 2017 , 15.7.5
- .NET Core SDK : 2.1.302
- Entity Framework Core : 2.1.1-rtm-30846
Any helps ? Thanks in advance .
来源:https://stackoverflow.com/questions/52319510/foreign-key-of-sql-scripts-generated-by-visual-studio-are-different