I have created a class library project in VS2010 ultimate with .net 4.0 and Entity fwk-4.1.
After adding a ref to the Entity framework dll, I am not able to use the Data Annotation attributes like 'Required' on class properties.
Could you please let me know what is the solution?
Required attribute is defined inside System.ComponentModel.DataAnnotations namespace and is available in System.ComponentModel.DataAnnotations assembly(dll)
1) Make sure to add a reference to System.ComponentModel.DataAnnotations assembly(dll)
2) Add a using statement to import the System.ComponentModel.DataAnnotations namespace in your class file.
using System.ComponentModel.DataAnnotations;
public class YourModel
{
[Required]
public string Name {set;get;}
}
Related
I'm using EF 6.4.4 in .NET 5, and want to associate a MyDbConfiguration with a MyDbContext. However, the only way to do this appears to declare MyDbContext with a DbConfigurationType attribute. For example:
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext
{
}
But an attribute is static, and I need to create MyDbConfiguration dynamically, via a dependency injection framework. Is there any way to do this?
I'm new in asp.net mvc and first time create new web application and in my project solution right click and add new project to solution and add class library to my project,and name it Datalayer,so in Datalayercreate new class and name it PageGroup,in PageGroup want to add System.ComponentModel.DataAnnotations to name space but get this error:
A namespace cannot directly contain members such as fields or methods...
I add entity frame work to datalayer but so not work!,how can i solve that problem?
this is my class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Datalayer
{
public class PageGroup
{
[Key]
public int GroupID { get; set; }
}
}
in [Key] get error.
I think you forgot to add assembly reference in your application.
Go to Reference->Add Reference-> Select System.ComponentModel.DataAnnotations assembly to your application.
Then use the namespace in your application
using System.ComponentModel.DataAnnotations;
Please find screenshot for reference
Add assembly system.ComponentModel.DataAnnotation in your current project.
Go to Add reference> Assemblies > Framework and choose system.ComponentModel.DataAnnotation
If this is not present in your current assembly then download it form nuget package manager.
Write click on your project.> manage nuget packages and search for System.componentmodel and select appropriate package and install.
I am using Xcode 7.2 and I have created a swift framework, I can import the framework without error but cannon access the class files of my framework.
In the DropDown class file set as public
Your Dropdown class is not exposed publicly or just check it is linked properly..
Check if your DropDown class is defined as public
public class DropDown
Maybe check if the init() in the DropDown class is marked as public
When I create a Model in MVC3, is there any workaround to set a custom value on a property like in the following example [MyCustomValue()] and how to get later that value programmatically?
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Web.Mvc
Imports System.Collections.Generic
Public Class MyModel
Public Property MyModelId As Integer
<StringLength(20), MyCustomValue(True)>
Public Property Name As String
[...]
Thanks to #AntP I found an example on how to set a custom attribute on a class (therefore also a Model) and how to get it programmatically
http://msdn.microsoft.com/en-us/library/tw5zxet9%28v=vs.100%29.aspx
Newbie-ish questions ahead:
Where should I put my View Model declarations?
I created a file called "ViewModels.cs" in my Models folder.
I created a few View Model classes, including one called RatingViewModel:
using System;
using System.Collections.Generic;var
using System.Linq;
using System.Web;
namespace Web.Model
{
public class ViewModels
{
public class RatingViewModel
{
public User Rater { get; set; }
public Rating Rating { get; set; }
}
}
}V
Now I'm trying to create a helper/service class function that returns a new RatingViewModel object.
I created a "RatingsHelpers.cs" file.
But when I try to crate a new RatingViewModel object, it has no idea what I'm talking about.
I can't figure it out. Am I missing some reference? How can I create/return a new View Model object from my helper/service class?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Web.Model;
namespace System.Web.Mvc
{
public static class RatingsHelpers
{
public static RatingViewModel functionname()
{ ..... }
But it doesn't know what "RatingViewModel" is...!?!?!?
I just know this should be obvious.
Pulling my hair out,
Johnny
To sum up:
You've created a ViewModels class inside the Web.Model namespace. This class declares a nested class called RatingViewModel
Inside the helper RatingsHelpers located in the System.Web.Mvc namespace you are trying to access the RatingViewModel class.
You could achieve this by using ViewModels.RatingViewModel but I wouldn't recommend you doing so. Nesting classes like this makes no sense.
Instead you could place each view model class into a separate .cs file and put it directly into the Web.Model namespace:
namespace Web.Model
{
public class RatingViewModel
{
public User Rater { get; set; }
public Rating Rating { get; set; }
}
}
Then inside the helper class use could use directly the RatingViewModel class.
The problem is that you put the RatingViewModel class inside the ViewModels class, which means that it's a child class of ViewModels. I'm not sure that's the behaviour you want.
If you want to call a child class you have to write ViewModels.RatingViewModel to reference the class.
Expanding on what scripni said. Having a RatingViewModel class nested inside a ViewModels class inside the Web.Models seems superfluous. You probably want to get rid of the outer ViewModels class.