Entify Framwork核心一对一的关系

时间:2016-10-08 17:09:05

标签: c# entity-framework ef-code-first asp.net-core entity-framework-core

我在使用Entity Framework核心创建简单的一对一关系时遇到了麻烦。 实体和OnModelCreating方法的覆盖如下所示:

Consider using the 'await' operator  to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. 
 System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'. 
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) 
    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName) 
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection) 
    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection) 
    at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection) 
    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
ClientConnectionId:f21894a0-4ae6-4b73-95fc-40a7804cf9cb
 Error Number:156,State:1,Class:15
 Incorrect syntax near the keyword 'WHERE'. 

当我传递命令Update-Database时,我收到以下错误消息:

import javax.swing.*;

public class MainClass extends JFrame {
    public static void main(String args[]) {
        new MainClass();
    }

    private JButton buttonOK;
    private JLabel label;

    MainClass() {
        this.setSize(350, 150);
        this.setTitle("Pizza Order");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JRadioButton small, medium, large, thin, thick;

        label = new JLabel();
        label.setText("Choose your size:");
        panel.add(label);

        ButtonGroup size = new ButtonGroup();
        ButtonGroup crust = new ButtonGroup();

        small = new JRadioButton("Small");
        medium = new JRadioButton("Medium");
        large = new JRadioButton("Large");
        size.add(small);
        size.add(medium);
        size.add(large);
        panel.add(small);
        panel.add(medium);
        panel.add(large);

        thin = new JRadioButton("Thin Crust");
        thick = new JRadioButton("Thick Crust");
        crust.add(thin);
        crust.add(thick);
        panel.add(thin);
        panel.add(thick);

        buttonOK = new JButton("OK");
        buttonOK.addActionListener(e -> buttonOKClick());
        panel.add(buttonOK);
    }

    public void buttonOKClick() {
        JOptionPane.showMessageDialog(MainClass.this, "Your pizza is being delivered!", "Pizza Delivery",
            JOptionPane.INFORMATION_MESSAGE);
    }
}

我应该检查什么? 谢谢!

2 个答案:

答案 0 :(得分:1)

我想。通常因为在一对一关系中实体框架无法推断出外键和主键。

您应该明确表达它们以确保正确创建外键。

modelBuilder.Entity<Blog>()
                .HasOne(parent => parent.BlogImage).WithOne(child => child.Blog)
                .HasPrincipalKey<Blog>(parent => parent.BlogId).HasForeignKey<BlogImage>(child => child.BlogForeignKey);

答案 1 :(得分:1)

感谢您试图帮助我。 似乎问题与SQL Server的版本有关。我不知道EFCore与SQL Server 2005不兼容。

谢谢! 最好的问候!