ASP.NET Core RC2將模型分離為跨平台項目

.net-core-rc2 asp.net-core c# entity-framework-core

我正在嘗試將模型分成他們自己的項目,以便在使用新的.net核心類庫的xamarin跨平台應用程序中使用。我正在提出的問題是:

錯誤圖片

我的解決方案結構如下:

解決方案結構圖

我的類庫project.json如下:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final"
  },

  "frameworks": {
    "net451": { },
    "netstandard1.5": {
      "imports": [
        "dnxcore50",
        "portable-net452+win81"
      ]
    },
    ".netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  }
}

我在Web應用程序中引用了類庫,但我不確定我是否正確地分離了模型。我試圖找到一個解決方案但是因為ASP.NET Core框架仍然很新,所以沒有太多文檔或指導如何將模型分成它自己的項目。我希望有人能就如何實現這一目標提供一些指導。

編輯

這是我的web應用程序project.json:

{
  "userSecretsId": "aspnet-Climbing.Web-d856be17-9b54-401e-98b2-4d6589fc7ff3",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
       "type": "build"
     },
     "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
       "version": "1.0.0-preview1-final",
       "type": "build"
     },
     "Microsoft.AspNetCore.Identity": "1.0.0-rc2-final",
     "Climbing.Domain": "1.0.0"
   },

  "tools": {
     "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
      },
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

在此處輸入圖像描述

在此先感謝您的幫助。

一般承認的答案

我發現這個解決方案很奇怪,這是一個不應該成為問題的命名空間問題。我確信底層的ASP.NET核心RC2框架中有一個錯誤,雖然我可能錯了?

當我將類庫中的ApplicationUser的命名空間從“Climbing.Web.Models”更改為“Climbing.Domain”時會出現問題 - 這將是正確的命名空間。一旦我將其更改回'Climbing.Web.Models'命名空間,問題就解決了。

編輯:深入了解並意識到我沒有在引用ApplicationUser的視圖中更新名稱空間。通過更新導入修復了此問題。


熱門答案

聽起來好像您沒有從Web項目到類項目的正確引用(或者,您缺少using語句。

要從Web項目引用類項目,請更新Web項目的project.json以及類似於以下內容的依賴項:

"dependencies": {
    "Climing.Domain": {
        "version": "1.0.0-*",
        "target": "project"
     },
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow