简体   繁体   English

在 VS 2015 中将程序集添加到类库

[英]Add Assembly to Class Library in VS 2015

Have used Visual Studio for years and are now trying to create my first project in Visual Studio 2015.多年来一直使用 Visual Studio,现在正在尝试在 Visual Studio 2015 中创建我的第一个项目。

I have create an MVC project and I am now interrested in adding a new project which will act as my data layer.我已经创建了一个 MVC 项目,现在我有兴趣添加一个新项目作为我的数据层。 Lets call the project MyProj.让我们将项目称为 MyProj。 So I created a new Web Class Library called MyProj.Data.所以我创建了一个名为 MyProj.Data 的新 Web 类库。 I also installed the newest EntityFramework (6.1.3) using NuGet to this project.我还使用 NuGet 为该项目安装了最新的 EntityFramework (6.1.3)。

Now I want to create a class which should act as my EntityTypeConfiguration like:现在我想创建一个类,它应该作为我的 EntityTypeConfiguration ,如:

public class GadgetConfiguration : EntityTypeConfiguration<Gadget>
{
  ....
}

Problem is that EntityTypeConfiguration is not recognised.问题是 EntityTypeConfiguration 无法识别。 Reharper says: Reference 'EntityFramework' and use 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration' Reharper 说:参考“EntityFramework”并使用“System.Data.Entity.ModelConfiguration.EntityTypeConfiguration”

Problem is that (as far as I see it) in folder References -> .NET Platform I can see my EntityFramework.问题是(据我所知)在文件夹 References -> .NET Platform 中我可以看到我的 EntityFramework。 So that should be referenced.所以应该参考。 But I cannot find System.Data.Entity in the references.但我在参考资料中找不到 System.Data.Entity。

When I try to Add Reference, my Assemblies tab say 'No items found'.当我尝试添加引用时,我的程序集选项卡显示“未找到项目”。 If I click the "Browse..." button I am told that "Project does not have target frameworks specified".如果我单击“浏览...”按钮,我会被告知“项目没有指定目标框架”。

Then I tried adding v4.5 to the MyProj.Data.xproj file (under ...) but that didn't change a thing.然后我尝试将 v4.5 添加到 MyProj.Data.xproj 文件(在 ... 下),但这并没有改变任何事情。

Then I changed the frameworks part of the project.json file from然后我将 project.json 文件的框架部分从

"frameworks": {
 "dotnet": { }
 }

to

"frameworks": {
"dotnet": { },
"dnx451": {
  "frameworkAssemblies": {
    "System.Data": "4.0.0.0",
    "System.Data.Entity": "4.0.0.0"
  }
}

Now I can see the System.Data and System.Data.Entity.现在我可以看到 System.Data 和 System.Data.Entity。 I can also add more assemblies from the References now.我现在还可以从参考中添加更多程序集。 But I still cannot USE them in my classes.但是我仍然不能在我的课堂上使用它们。 Neither by typing既不通过打字

using EntityFramework

or或者

using System.Data

or by referencing directly like或者直接引用像

public class GadgetConfiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Gadget>

How can I reference/use the needed assembly?如何参考/使用所需的程序集?

Okay, solution was simple... I Created a Web Class Library which is to be used when you want to create a class library as a NuGet package.好的,解决方案很简单...我创建了一个 Web 类库,当您想要将类库创建为 NuGet 包时将使用它。 This was NOT what I wanted (you couldn't know from my initial description).这不是我想要的(你无法从我最初的描述中知道)。

So solution was to drop the Web Class Library and create a Windows Class Library instead.所以解决方案是删除 Web 类库并创建一个 Windows 类库。 It now works!它现在可以工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM