How can I resolve this? The unit type exists in tw

2019-06-17 16:04发布

I am currently going through a tutorial using Visual Studio 11 beta. When trying to set the max length of a field value in one of my classes:

[MaxLength(50)]
public string LastName { get; set; }

It errors out and wont let me compile because the MaxLength() function exists in two places:

Error 4 The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute' exists in both 'c:\Users\me\Documents\Visual Studio 11\ContosoUniversity\packages\EntityFramework.4.1.10331.0\lib\net40\EntityFramework.dll' and 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'

I have tried to remove both files but that just causes more issues because other code in my project is dependent upon them.

Is there a way I can tell it to use one or the other?

All of these approaches don't seem to be working for me.. Refer to the comments under the answers.. Any other ideas?

Thanks

7条回答
该账号已被封号
2楼-- · 2019-06-17 16:10

Just Uninstall EntityFramework from packages and reinstall it(EntityFramework). It works for me. Just follow the steps mentioned below:

1.Right click on reference 2.Click on manage nugetpackages

1.Right click on reference 2.Click on manage nugetpackages.

3.Click on Uninstall

3.Click on Uninstall

  1. go in online section and type entity frame work in search box 5. Click on install button

4. go in online section and type entity frame work in search box 5. Click on install button

查看更多
来,给爷笑一个
3楼-- · 2019-06-17 16:12

Try using extern alias http://msdn.microsoft.com/en-us/library/ms173212.aspx to differentiate between the two assemblies

Also check out http://bartdesmet.net/blogs/bart/archive/2006/10/07/4502.aspx near the bottom of the page is an example

查看更多
你好瞎i
4楼-- · 2019-06-17 16:17

This question is now the top SO answer for this question so I figured I would answer it generally here.

The The type 'BLAH' exists in both error often pops up in the following occassions:

1. DUPLICATE FILES - (often very simple) This is notoriously the case with .dll files. In most cases of duplication, deletion of one of the duplicate files is the easiest and best solution

2. NON-DUPLICATE FILES - (more complicated, such as the original poster's case) you unfortunately need to edit and often recompile/find alternate versions of the files so that conflicts like this do not arise. Luckily more often than not, qualifying the attribute with the same namespace will fix conflicts like this.

For example if your code is conflicted on something called MaxLength that is mentioned in several files, then qualify it on necessary files to make it very clear which MaxLength you want to use like so: System.ComponentModel.DataAnnotations.MaxLength. This should help to clear things up, so nothing gets conflicted when trying to run your code

查看更多
地球回转人心会变
5楼-- · 2019-06-17 16:23

Use using at the top of your code:

using MaxLength = System.ComponentModel.DataAnnotations
查看更多
别忘想泡老子
6楼-- · 2019-06-17 16:23

In case, if you are getting the same error in latest environment (VS 2017/.NET Framework 4.6.x) and with entityframeworks like 6.1 or 6.2, here is the solution;

Downgrade your entityframework to 6.0. It'll work.

查看更多
不美不萌又怎样
7楼-- · 2019-06-17 16:24

MaxLength is not a function, it's an Attribute.
You can use the using directive in each file to specify the current correct context.
Or just type the full namespace, e.g. System.ComponentModel.DataAnnotations.MaxLength

查看更多
登录 后发表回答