实体框架,复合键不会被创建

时间:2015-08-24 07:33:20

标签: c# sql-server entity-framework

我有以下课程:

public class LWService extends WallpaperService {

    static int engineCounter;

    /** 
     * Will show the bug with calling {@link #onSurfaceCreated(SurfaceHolder)} 
     * and other surface callbacks after {@link #onDestroy()}. 
     *  
     */ 
    private class LWEngineTest1 extends Engine {

        public int id;

        public LWEngineTest1()
        {
          id=++engineCounter;
        }

        /** 
         * Will be set to <code>true</code> in {@link #onDestroy()}. 
         */ 
        private boolean mAlreadyDestroyed = false;

        /** 
         * Log debug level message with adding object instance info to better 
         * LogCat readability. 
         *  
         * @param message 
         *            message to log 
         */ 
        private void logD(final String message) {
            Log.d("LW_BUG_TEST", this.toString() + ":" + message);
        } 

        /** 
         * Log error level message with adding object instance info to better 
         * LogCat readability. 
         *  
         * @param message 
         *            message to log 
         */ 
        private void logE(final String message) {
            Log.e("LW_BUG_TEST", this.toString() + ":" + message);
        } 

        @Override 
        public void onCreate(SurfaceHolder surfaceHolder) {
            logD("onCreate() engineId="+id); 
        } 

        @Override 
        public void onDestroy() { 
            logD("onDestroy() engineId="+id); 
            mAlreadyDestroyed = true;
        } 

        @Override 
        public void onSurfaceCreated(SurfaceHolder holder) {
            logD("onSurfaceCreated() engineId="+id); 
            if (mAlreadyDestroyed) {
                logE("onSurfaceCreated() after onDestroy()"); 
            } 
        } 

        @Override 
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            logD("onSurfaceChanged() engineId="+id); 
            if (mAlreadyDestroyed) {
                logE("onSurfaceChanged() after onDestroy()"); 
            } 
        } 

        @Override 
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            logD("onSurfaceDestroyed() engineId="+id); 
            if (mAlreadyDestroyed) {
                logE("onSurfaceDestroyed() after onDestroy()"); 
            } 

            try { 
                // To reveal the bug, without this line you may not got the 
                // issue. Of course it is absolutely synthetic but allow to get 
                // the issue guaranteed 
                Thread.sleep(4000);
            } catch (InterruptedException exc) {
            } 
        } 
    } 

    @Override 
    public Engine onCreateEngine() {
        return new LWEngineTest1(); 
    } 
} 

(缩写为可读性)

如您所见,GoodsIssueProcess包含产品和交货单代码(基本上只是一个数字)。根据“Key”关键字的规范,我希望获得ProductId和DeliveryNote的复合主键。然而,我得到的是DeliveryNote的正常主键。 Product列甚至获取Nullable属性和Product表的外键关系(很好)。

你有什么想法我做错了吗?我也试过分配以下内容:

public class GoodsIssueProcess
{

    [Key, Column(Order = 1), MaxLength(128)]
    public string DeliveryNote { get; set; }

    [Key, Column(Order = 2)]
    public Product Product { get; set; }
}

public class Product
{
    // the unique ID of the product
    [Key]
    public int Id { get; set; }

    [Required, MaxLength(36)]
    [Index("IX_ArticleNumber", 1, IsUnique = true)]
    public string ArticleNumber { get; set; }
}

结果是相同的数据库布局(只有一个主键,没有复合键)。我正在研究EF 6.0和MSSQL Server 2012。

编辑: 停止! - 显然版本#2工作(具有public int ProductId的版本)。但是,第一个版本不应该也能正常工作吗?

1 个答案:

答案 0 :(得分:1)

复合键的关键部分应该是可以在SQL中转换为基本类型的类型,Product类型无法转换为基本类型,但ProductId可以转换为int在SQL中 复合键必须是:

public class GoodsIssueProcess
{

    [Key, Column(Order = 1), MaxLength(128)]
    public string DeliveryNote { get; set; }

    [Key, Column(Order = 2)]
    public int ProductId { get; set; }
}