I have the following partial class, which provides Metadata to my Database first models.
namespace Model.Metadata.Routing
{
[MetadataType(typeof(RoutingMetadata))]
public partial class Routing
{
}
public partial class RoutingMetadata
{
[DefaultValue("%")]
public string Slot { get; set; }
[Required(ErrorMessage = "This field is requied")]
[DefaultValue(0)]
public int BlockStart { get; set; }
[Required(ErrorMessage = "This field is requied")]
[DefaultValue(499)]
public int BlockEnd { get; set; }
[DefaultValue(-1)]
}
}
Now I want to add a constructor for the Routing Class to default my values,
public Routing()
{
Slot="%";
}
Where do I add the constructor?
[EDIT]
Other half of partial Class Routing
public partial class Routing
{
public string Slot { get; set; }
public int BlockStart { get; set; }
public int BlockEnd { get; set; }
}
You can put the constructor in any one of the partial class definitions, it's up to you where you think it makes the most logical sense.
Of course, all parts of a partial class definition need to have the same class name and namespace, or they are different classes. Routing and RadioRouting are not the same class, because they don't have the same name.
Related
I have a domain class like this,
public class DomainClassExample
{
public int ClientAssessmentId { get; set; }
public int IfaId { get; set; }
}
I want to get the data in this domain class in another model class as a list.
Is there any way by which this can be done?
You can add the model to any other class as a proprty.
public class AnotherModelClassExample
{
//...other properties
public IEnumerable<DomainClassExample> DomainClassExampleList { get; set; }
public AnotherModelClassExample(){
DomainClassExampleList = dbcontext.DomainClassExamples.ToList();
}
}
Here is EF Models class, Auto generate.
namespace MySys.Models
{
using System;
using System.Collections.Generic;
public partial class CustomerInfo
{
public System.Guid CustomerInfoID { get; set; }
}
}
Here is My partial class
public partial class CustomerInfo
{
public string CustomerType { get; set; }
public Nullable<System.Guid> OperatorUserID { get; set; }
}
But When I used this code to get list, the value of CustomerType is null.
List<CustomerInfo> CustomerInfoList = new List<CustomerInfo>(db.Database.SqlQuery<CustomerInfo>("EXEC usp_GetCustomerInfoList #PageSize,#PageIndex,#WhereStr,#OrderbyStr,#TotalRecord output", param_pagesize, param_pageindex, param_wherestring, param_orderstr, param_totalrecord));
Anyone can tell me how to do it correct?
The two files is in the same assemblies.
Thanks.
The partial class is used to extend the entity type and those properties will be ignored during query.
You can try creating a derived type instead.
public partial class CustomerInfo
{
public string CustomerType { get; set; }
public Nullable<System.Guid> OperatorUserID { get; set; }
}
public class CustomCustomerInfo : CustomerInfo {}
And map the query to the derived type.
SqlQuery<CustomCustomerInfo>("EXEC usp_GetCustomerInfoList ...")
i have a class the sales and product, which have the same declaration of object, these two class is auto generated by the server, all i want is set the annotation of them so that it can be reusable all the time, how can i set the MetadataType of this two into one,
From Sales
public partial class Sales
{
public string Currency { get; set;}
}
From Product
public partial class Product
{
public string Currency { get; set;}
}
namespace Validation.Access
{
[MetadataType(typeof(TransferModuleValidation))]
public partial class Product { }
public partial class TransferModuleValidation
{
[MaxLength(3, ErrorMessage = "Must be less than or 3 characters")]
public string Currency { get; set;
}
}
how can i set Sales in that example?
do i have to inherit class (Product) to Sales?
Add the MetadataType to the other class...
namespace Validation.Access
{
[MetadataType(typeof(TransferModuleValidation))]
public partial class Sales { }
[MetadataType(typeof(TransferModuleValidation))]
public partial class Product { }
public partial class TransferModuleValidation
{
[MaxLength(3, ErrorMessage = "Must be less than or 3 characters")]
public string Currency { get; set;
}
}
I have an auto generated class with a property on it. I want to add some data annotations to that property in another partial class of the same type. How would I do that?
namespace MyApp.BusinessObjects
{
[DataContract(IsReference = true)]
public partial class SomeClass: IObjectWithChangeTracker, INotifyPropertyChanged
{
[DataMember]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _name;
}
}
and in another file I have:
namespace MyApp.BusinessObjects
{
public partial class SomeClass
{
private SomeClass()
{
}
[Required]
public string Name{ get; set; }
}
}
Currently, I get an error stating that the name property already exists.
Looks like I figured out a different way similar to the link above using MetadataTypeAttribute:
namespace MyApp.BusinessObjects
{
[MetadataTypeAttribute(typeof(SomeClass.Metadata))]{
public partial class SomeClass
{
internal sealed class Metadata
{
private Metadata()
{
}
[Required]
public string Name{ get; set; }
}
}
}
I'm using the below to also support multiple foreign keys in the same table that refer to the same table. Example, the person has two parents (Father and Mother) who are both Person class.
[MetadataTypeAttribute(typeof(SomeClassCustomMetaData))]
public partial class SomeClass
{
}
public class SomeClassCustomMetaData
{
[Required]
public string Name { get; set; }
[InverseProperty("Father")]
public virtual Parent ParentClass { get; set; }
[InverseProperty("Mother")]
public virtual Parent ParentClass1 { get; set; }
}
Is there a way I can have a generated code file like so:
public partial class A
{
public string a { get; set; }
}
and then in another file:
public partial class A
{
[Attribute("etc")]
public string a { get; set; }
}
So that I can have a class generated from the database and then use a non-generated file to mark it up?
Here is the solution I have been using for such cases. It is useful when you have auto-generated classes that you want to decorate with attributes. Let's say this is the auto-generated class:
public partial class UserProfile
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
And let's say, I would like to add an attribute to specify that UserId is the key. I would then create a partial class in another file like this:
[Table("UserProfile")]
[MetadataType(typeof(UserProfileMetadata))]
public partial class UserProfile
{
internal sealed class UserProfileMetadata
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
}
}
I've seen something like this done in an article by Scott Guthrie (near the end of it) - didn't try it myself, though.
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
[MetadataType(typeof(Person_Validation))]
public partial class Person
{
// Partial class compiled with code produced by VS designer
}
[Bind(Exclude="ID")]
public class Person_Validation
{
[Required(ErrorMessage = "First Name Required")]
[StringLength(50, ErrorMessage = "Must be under 50 characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")]
[StringLength(50, ErrorMessage = "Must be under 50 characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "Age Required")]
[Range(0, 120, ErrorMessage = "Age must be between 0 and 120")]
public int Age { get; set; }
[Required(ErrorMessage = "Email Required")]
[Email(ErrorMessage = "Not a valid email")]
public string Email { get; set; }
}
This is my answer
different class files or you can combine the metadatas in a same file but keep the namespace the same..so they can see each other obviously.
keep in mind when you update your model like add more columns you have to update the project class too.
--your model class
public partial class A {
public string a {get; set;}
}
--your project class
public class Ametadata {
[Attribute("etc")]
public string a {get; set;}
}
[MetadataType(typeof(Ametadata))]
public partial class A
{
}
You need to define a partial class for your A class just like below example
using System.ComponentModel.DataAnnotations;
// your auto-generated partial class
public partial class A
{
public string MyProp { get; set; }
}
[MetadataType(typeof(AMetaData))]
public partial class A
{
}
public class AMetaData
{
[System.ComponentModel.DefaultValue(0)]
public string MyProp { get; set; }
}
Not as such; the compiler will complain that the member is defined in multiple parts. However, as the use of custom attributes is reflective in nature, you could define a "metadata" class and use it to contain decorators.
public class A
{
public string MyString;
}
public class AMeta
{
[TheAttribute("etc")]
public object MyString;
}
...
var myA = new A();
var metaType = Type.GetType(myA.GetType().Name + "Meta");
var attributesOfMyString = metaType.GetMember("MyString").GetCustomAttributes();