If I have the code:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
How do I convert it to the code below at runtime or compile time?
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
Can I do this using DynamicProxy? Cecil? or some other open source project?
Related
I have a table named User in my database. I also have a .net core project where authentication is built-in. I want to connect to my database and after scaffolding reveals my User class, I want it to inherit from IdentityUser.
After scaffolding went well, I tried to inherit from IdentityUser.
public partial class User :IdentityUser<int>
{
public int Id { get; set; }
public string Country { get; set; }
public string PersonalId { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public byte[] Idimage { get; set; }
public bool? EmailConfirmed { get; set; }
public bool? Smsconfirmed { get; set; }
}
I can not see Identity Fields like PasswordHash, PhoneNumberConfirmed and so on, in my database. Specifically, in User table.
Just been writing up my models and dbcontext using a code first approach for EFCore and i've hit a small problem... specifically with classes and generating migrations.
It seems with entityframework any class is seen as an entity/table (my assumptions so far) but what if I want a class to be a list of fields extended onto my entity?
For example:
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public Address AddressDetails { get; set; }
}
public class Address {
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
}
How can I mark the address class as additional fields to the person entity as opposed to a separate entity?
Cheers,
Mark
So, I've been looking at all kinds of examples of a WCF class that is to be serialized. I'd like to define all my DataContracts and DataMembers in interfaces and then allow implementation that suits the environment. Is this possible, and if so, are there drawbacks to this approach?
[DataContract]
public class Contact
{
[DataMember]
public int Roll { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public int Age { get; set; }
}
Can instead do this:
[DataContract]
public interface IContact
{
[DataMember]
int Roll { get; set; }
[DataMember]
string Name { get; set; }
[DataMember]
string Address { get; set; }
[DataMember]
int Age { get; set; }
}
public class Contact :IContact
{
public int Roll { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
I am using ASP.NET MVC Razor Entity Framework Code First C#
Class - A
public class Om_Category
{
[Key]
public int CategoryID { get; set; }
public String CategoryName { get; set; }
public String CategorySanitized { get; set; }
public Boolean IsActive { get; set; }
public DateTime CreationDate { get; set; }
}
Class - B
public class Om_CategorySkills
{
[Key]
public Int32 SkillID { get; set; }
public String Skill { get; set; }
public String SkillSanitized { get; set; }
public DateTime CreationDate { get; set; }
public Boolean IsActive { get; set; }
public Om_Category Category { get; set; }
}
When I try to create the record for table Om_CategorySkills. It says
cannot save the duplicate value in Om_Category table.
This is happening because I am sending the Om_Category class object in Om_CategorySkills class object because there are some fields in Om_Category class that are mandatory.
So I am passing the Om_Category class object also in Om_CategorySkills class object. Is there any way to fix this issue ?
Your navigation properties doesn't seem to be right.. Can you try (I didn't test),
public class Om_Category
{
[Key]
public int CategoryID { get; set; }
public String CategoryName { get; set; }
public String CategorySanitized { get; set; }
public Boolean IsActive { get; set; }
public DateTime CreationDate { get; set; }
public virtual Om_CategorySkills CategorySkills{ get; set; }
}
public class Om_CategorySkills
{
[Key]
public Int32 SkillID { get; set; }
public String Skill { get; set; }
public String SkillSanitized { get; set; }
public DateTime CreationDate { get; set; }
public Boolean IsActive { get; set; }
public int CategoryID {get;set;}
public virtual Om_Category Category { get; set; }
}
I see that your Om_CategorySkills object is lacking an Int32 Om_CategoryId property to be used as foreign key. I would also add a virtual modifier to the navigation property Category, in order to allow for lazy loading.
I think that it may be the case that the category object in your new/edited skill is already in the database, but was not the one retrieved by the context, so the context believes you are trying to save a new category with the Id of an existing one.
You should not try to save a skill object with a category object with no changes. Otherwise, the category object should be the one attached to the context.
I'm having an issue with EF code first and Odata.
If I navigate to the metadata then I find that all of my relationships are defined like this:
<NavigationProperty Name="Jobs" ToRole="Jobs" FromRole="Applicant" Relationship=".Applicant_Jobs"/>
Notice the full stop before the relationship name?
This stops me from adding a service reference to a Console Application. It gives a custom tool error:
Schema specified is not valid. Errors: The relationship attribute is invalid - The value '.Applicant_Jobs' is invalid according to its datatype.
My data contract is defined here:
[DataContract]
public class Applicant
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Forename { get; set; }
[DataMember]
public string Surname { get; set; }
[DataMember]
public string EmailAddress { get; set; }
[DataMember]
public string PassPhrase { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string County { get; set; }
[DataMember]
public string Country { get; set; }
[DataMember]
public DateTime CreationDate { get; set; }
[DataMember]
public ICollection<Job> Jobs { get; set; }
}
[DataContract]
public class Job
{
[DataMember]
public Applicant Applicant { get; set; }
[DataMember]
public JobStatus JobStatus { get; set; }
[DataMember]
public int ID { get; set; }
[DataMember]
[ForeignKey("Applicant")]
public int ApplicantID { get; set; }
[DataMember]
[ForeignKey("JobStatus")]
public int JobStatusID { get; set; }
[DataMember]
public string JobTitle { get; set; }
[DataMember]
public string WebsiteURL { get; set; }
[DataMember]
public string Reference { get; set; }
[DataMember]
public string Location { get; set; }
[DataMember]
public string Salary { get; set; }
[DataMember]
public string ContactName { get; set; }
[DataMember]
public string ContactEmail { get; set; }
[DataMember]
public string ContactPhone { get; set; }
[DataMember]
public string Pros { get; set; }
[DataMember]
public string Cons { get; set; }
[DataMember]
public string GeneralNotes { get; set; }
[DataMember]
public DateTime CreationDate { get; set; }
[DataMember]
public bool Archived { get; set; }
[DataMember]
public ICollection<JobProgression> JobProgress { get; set; }
}
As you can see, I'm not putting a full stop anywhere, but for some reason one is generated for each relationship in my model, meaning the custom tool fails.
It turns out the contract needs to be in a namespace. That's what the provider was trying to put in front of the dot.