简体   繁体   中英

.NET 4.0 substitutes for .NET 4.5 namespaces

There is some code for an MVC application which was built using .NET 4.5 as a framework in VS 2012. My current system forces me to work on VS 2010. I managed to open the VS2012 solution in my VS2010, but the thing is that VS2010 supports only up to .NET 4.

There are a few functions in the code which use dll files which are available only for .NET 4.5, for example System.ComponentModel.DataAnnotations.Schema .

So, are there any substitute functions/attributes which are available in .NET 4, which I could use to do the same that is being done on .NET 4.5 right now?

This is my current code using .NET 4.5:

 [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    }

As you can see DatabaseGeneratedAttribute is available under the System.ComponentModel.DataAnnotations.Schema namespace, which is a part of .NET 4.5.

Any suggestions on what the corresponding functions/attributes that could be used in .NET 4 to represent the same logic?

Note: In the code snippet given above, I get errors on Table and DatabaseGeneratedAttribute as

The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?)

and

The type or namespace name 'DatabaseGeneratedAttributeAttribute' could not be found (are you missing a using directive or an assembly reference?)

respectively. So, I am guessing that I just need to find the corresponding classes in NET 4.0 and things would fall into place. Your help is deeply appreciated.

I saw the same issue after changing from .NET 4.5 to 4.0. According to this article "there is a special version of the Entity Framework assembly" for .NET 4.0 containing DataAnnotations , which have otherwise been incorporated into .NET 4.5.

Reinstalling Entity Framework made System.ComponentModel.DataAnnotations.Schema work again. Reinstall by typing the following in Package Manager Console:

Uninstall-Package EntityFramework
Install-Package EntityFramework

In this article , Author claims to be able to use System.ComponentModel.DataAnnotations.Schema in an MVC4 app using VS2010 SP1.

Let us know if this works out for you.

Henrik's solution also works for the opposite situation. I had an issue with going from .net 4.0 to .net 4.5.1 to support MVC 5.2.3. I got the same errors. Uninstalling and Re-Installing Entity Framework solved the problem in this situation as well:

Uninstall-Package EntityFramework
Install-Package EntityFramework

The Package Manager Console didnt work for me. I had to go Manage Nuget packages to get this issue resolved for me. I am using VS.net 2013

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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