I want to change my CustomAttribute usage in this way, I'm using it to refer to the search parameters of an autocomplete dropdown
now:
[EntitySearchDropBox(Id="Id",SearchColumns="Name")]
public virtual class MyTarget
But i hate strings for defining entity properties and the best solution for me is passing AttributeTarget property without writing free strings.
[EntitySearchDropBox(Id=MyTarget.Id,SearchColumns=MyTarget.Name)]
public virtual class MyTarget
MyTarget example
public class MyTarget{
public int Id {get; set;}
public string Name {get; set;}
public string otherProperty { get; set; }
//etc..
}
Related
I am have a method (GetUser()), method return type is an interface (IUser) and I have a class (User) include same fields with IUser. I want to bind GetUser methods result to User object.
I am using c#.
Method :
IEnumerable<IUser> GetUser();
Interface :
public interface IUser {
public string Id{ get; set;}
public string Name{ get; set;}
public string Username{ get; set;}
public IAddress Address{ get; set;}
//And more fields
}
Class :
public class User {
public string Id{ get; set;}
public string Name{ get; set;}
public string Username{ get; set;}
public Address Address{ get; set;}
//And more fields
}
I tried the following cases and not works.
IEnumerable<User> user = AnyLibrary.GetUser() as IEnumerable<User>;
IEnumerable<User> user = (IEnumerable<User>) AnyLibrary.GetUser();
Is it possible and how can i do this ?
The real solution to the problem is to inherit the IUser class as Adam Vincent and stuartd mentioned.
But If don't want to inherit from the Iuser class.Another solution that comes to my mind json serialize to GetUser() method's result and deserialize to User object.
string users = JsonConvert.SerializeObject(AnyLibrary.GetUser());
IEnumerable<User> c_user = JsonConvert.DeserializeObject<IEnumerable<User>>(users);
This solutions works for me well.
I'm using C# and .NET Core with MySql and Entity Framework.
I have an object with a collection of properties. Like this:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Property> Properties { get; set; }
}
public class Property
{
public int Id { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
In this case in the database, I should have tables Products, Properties (where property is described, like name and some additional info), and link table ProductProperties, storing product Id, property Id and Value.
But I can't figure out how to do this with a code-first approach.
How could I implement it with code first?
Is it a good way to create one more entity PropertyValue and store it under Product?
Something like this should give you a 1-to-many relationship, although you need to give Value a type, like string to store it in the database, often for dynamic solutions like this you would then maybe add a type to specify the type to deserialize into, but since you then deserialize anyway you could also just add things as json or something else in the db.
public class Product
{
public int Id {get; set;}
public string Name{get; set;}
public ICollection<Property> Properties{get; set;}
}
public class Property
{
public int Id {get; set;}
public string Name {get; set;}
public string Value {get; set;}
public int ProductId {get; set;}
}
Unless you are making a very dynamic system, it doesn't seem right to have the properties as a table, depends a lot of what you are making, and maybe key-value db might be a better tool for the job if thats what your main problem is, as with most complicated things, it depends.
This example is a convention based approach, which is why properties like ProductId have to be called exactly that. You can look at EntityTypeConfigurations if you want more control of names and relationships and such, or use data annotations to achieve the same job.
Ok so create a table like this:
public class ProductProprties
{
public int ProductId {get; set;}
public Product Product {get;set;}
public int PropertyId {get; set;}
public Property Property {get;set;}
//other props
}
If you are using EntityFramework Core, then you have to add this to your databse context as well:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ProdcutProprties>().HasKey(x => new { x.ProductId , x.PropertyId });
}
I'm creating a database first web api using entity framework. I created my models by creating ADO.NET Entity Models that were populated automatically with the fields on the tables that I selected. I want to be able to take some of those fields and group them in a custom object so when the xml is displayed, they are displayed as a child node.
An example is if I had the class with the following attributes
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public string street{get; set;}
public string city{get; set;}
public string zip{get; set;}
}
I want it to be something like this instead
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public string address{get; set;}//where address contains street, city, and zip
}
How would I go about in implementing this new Person class and populating the data of the custom object with my dbcontext
It is impossible to change three string properties to the one string property. How you can divide Address property to the street city and zip for example in case when street has two words?
You can create Address class (with three properties) and use it your Person class. But you will have problems when you try to regenerate/update your model - in such case is better to use code first approach.
Belongs-to Relationship
I think what you're looking for is a relationship between Person and a new class called Address.
To reference your new Address class inside Person, you would do the following:
public class Person
{
...
public Guid AddressId { get; set; }
public Address { get; set; }
}
Entity Framework will then associate an Address with the given AddressId with a Person.
Inside Address you can do something like this:
public class Address
{
public string City { get; set; }
public string Street { get; set; }
...
}
Eager Loading
To make sure an Address is loaded when a Person is loaded, you can use Entity Framework's Eager Loading capabilities, such as:
context.Person.Include(x => x.Address)
Use enumerable List like
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public List addresses{ get; set; }
}
public class Address
{
public string street{get; set;}
public string city{get; set;}
public string zip{get; set;}
}
I have two related entities called DataTag and TagSource that look like the following:
public class DataTag : BaseModel
{
[Column("DataTagId")]
public override Guid ID { get; set; }
public string Tag { get; set; }
public Guid TagSourceId { get; set; }
public TagSource TagSource { get; set; }
}
public class TagSource : BaseModel
{
[Column("TagSourceId")]
public override Guid ID { get; set; }
public string Description { get; set; }
public bool IsInternal { get; set; }
public string Source { get; set; }
public ICollection<DataTag> DataTags { get; set; }
}
I am allowing the user to Include the navigation properties through the url like "/api/DataTags?Include=TagSource". The problem is when I include the TagSource, it also includes the collection of DataTags in that object which I don't want unless the user specifies it (For example "/api/DataTags?Include=TagSource.DataTags". Is there any way to stop that property from being loaded when I include the TagSource? I have tried making the properties virtual and turning lazy loading off globally but that didn't work. The reason I haven't marked them virtual is because I am using AutoMapper and I only want to include the navigation properties that the user specifies.
As in the comments you need to create a DTO object. There is a good article here detailing how to do this with WebAPI
http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5
Edit.
The problem with this is you will need a lot of different DTO objects for each possible outcome which could become messy. If your return type is JSON you can add this attribute to your properties:
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
Firstly : Apologies for my English.
Secondly : I had the same issue with a code first database model that creates foreign keys this way : public virtual Collection<Object> Objects {get; set;}
and I found a workaround by setting the property setter as private:
public virtual Collection<Object> Objects {get; private set;}
Then the EF cannot populate the Objects collection because with a private set you can only assign a value in constructors.
I have a class named "City"
public class City
{
public int ID {get; set;}
public int StateID {get; set;}
public int CountryID{get; set;}
public string Name{get; set;}
.......
}
and i have an asp.net page named CityAdd.aspx, in this page i want to create a collection of city class that can be store in the viewstate.
Is it possible to make a Generic Collection Serializable?
do as below, add Serializable attribute
[Serializable]
public class City
{
}