foreign key of sql scripts generated by visual studio are different

人盡茶涼 提交于 2019-12-11 04:24:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!