How to specify set of valid string values for a property? - .net-core

I have a class:
public class Product
public Guid Guid {get;set;}
[Required]
public string Brand { get; set; }
}
I want to specify what values are legal values for property Brand, for instance "Audi,BMW,Mercedes".
Is that is possible to do without custom validator?

Related

How to map a Point in the entity framework?

I am developing an API in the .net core and using the Framework entity.
My bank already existed and in one of the tables I have a Point type field to store coordinates (Spatials).
I'm not using any automatic approach (Ex: code First, DataBase First ...), I am modeling my classes myself.
To map this Point field I did it as in primitive types, I believe to be wrong, and I'm getting an error:
System.InvalidOperationException: 'The property 'Address.LatLong'
could not be mapped, because it is of type 'Point' which is not a
supported primitive type or a valid entity type. Either explicitly map
this property, or ignore it using the '[NotMapped]' attribute or by
using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
public class Address:BaseModel
{
[Required]
[StringLength(30)]
public string Street { get; set; }
[Required]
public int Number { get; set; }
[StringLength(45)]
public string Observation { get; set; }
[Required]
[StringLength(20)]
public string PostalCode { get; set; }
[ForeignKey(nameof(City))]
[Required]
public int CityId { get; set; }
public virtual City City { get; set; }
public Point LatLong { get; set; } //this is the field
}
if you wanna use that point for other address , so u should add something like this to your point Class :
public virtual ICollection<Address> Address{ get; set; }
and if you dont wanna use that point for another adress , u can bring Lat and Long to your adress class.
by the way i think add this public virtual ICollection<Address> Address{ get; set; } should fix your problem

Cannot generate Id using Data annotation (EF6)

I'm new at EF6 and Asp.net MVC5.
There's problem of generating a unique ID automatically when I try to create my entities using the code first approach.
Consider an entity like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PostId { get; set; }
[Required]
[MaxLength(100), MinLength(5)]
public string Title { get; set; }
public string Content { get; set; }
public string Thumbnail { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
When I put [DatabaseGenerated(DatabaseGeneratedOption.Identity)] on PostId or even remove it. I was always get an exception error like this:
“Cannot insert the value NULL into column ‘PostId'”.
“column does not allow nulls. INSERT fails.
I don't know why EF6's always try to insert a null value to the Id column.
Then later, I found a solution.
I changed
DatabaseGeneratedOption.Identity to DatabaseGeneratedOption.None
and the problem was solved.
However, this solution doesn't seem to work like I expected.
the values inserted to the column are always the same. It's not unique.
With EF core, everything is just simple, I don't need DatabaseGenerated,just leave it to convention. But with EF6, I'm stuck. What I want is the Id field must be unique and increase everytime it inserted to the database.
Can someone please help me this?
I'm new to EF also, but I just create an Id field and it is inserted automatically by SQL Server, such as this model in my current project. The Id field populates as expected when I add new rows:
public class BlogPosts
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string ShortPost { get; set; }
[Required]
public string Post { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public DateTime Updated { get; set; }
}
When you apply DatabaseGeneratedOption.Identity to your key property you are telling EF that the database will generate the identity. From the error it sounds like your database PostId column is not configured as an identity or primary key.
If you manually created the table and columns in the database you should ensure the configuration is at least compatible, or you should use migrations to ensure the database is configured in a good state for your EF model.

Using the same generic foreign key field (KeyId) along with a (Type) field for a single table in EntityFramework

I have a class called Address with the following properties:
public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public int KeyId { get; set; }
public AddressType Type { get; set; }
The plan is for AddressType to be an enum of either: Customer, Vendor, or Location and the KeyId to be the Foreign Key from either the Customer, Vendor, or Location.
Is this something that can be done correctly with Entity Framework or should I be making 3 separate classes CustomerAddress, VendorAddress, LocationAddress.
You need to generate an entity inheritance structure from a single database table. Can achieve that by using a Table per Type approach
TBT Entity Framework
Group the common properties into a "Base" entity, and then use a discriminator (in your case the AddressType) for specializing the object.
I looks like you have three tables Customer, Vendor, or Location and you want to save Address for each.
Then instead of adding AddressType in Address table, you should include AddressID in each table as Foreign key and must have a Address navigation property in each table.

Associate DocumentDB's document id to class property

Is there a way to associate a Document's unique ID to a property in my class?
For eg:
public class Product
{
//Associate this to ID
public string ProductId { get; set; }
public string UserId
{
get;
set;
}
public string ProductName
{
get;
set;
}
}
The thing is, I dont want to create and maintain unique Ids for my objects but reuse the Id that DocumentDB creates after writing a document.
if you decorate your unique id using JsonProperty attributes then you can use your fields as the unique id, instead of having another id field on every document.
and example is;
[JsonProperty(PropertyName="id")]
public string ProductId { get; set; }

Referencing the creator of a page blog (the connected user) in my entity

I have an entity for my accessing my pages (pages of a blog).
Here it is:
public class Page
{
[Key]
public int PageId { get; set; }
public string AuthorName { get; set; }
[ForeignKey("AuthorName")]
public virtual MembershipUser Author { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Content { get; set; }
public DateTime? PublishDate { get; set; }
public bool Published { get; set; }
public DateTime LastModified { get; set; }
}
As you can see, I would like to keep a reference to the person who created the page (in fact this is the connected user). So I defined an Author member in my class (of type MembershipUser). I also try to define the foreign key but it doesn't work because there is no primary key in the MembershipUser entity. I cannot add one to this entity because if is an aspnet object.
How can I proceed in order to have a reference to the creator of the page in my Page entity?
Thanks.
You can extend the MembershipUser by creating a CustomMembershipUser that will inherit from MembershipUser.
You can add any fields you want to your CustomMembershipUser, you will then also have to create a table of your own with both the original fields and your extra fields.
Here is some documentation that describes how you can do this.
What ORM are you using?
Anyway, you won't be able to reference a class that is not mapped in your ORM. You could create an entity like SystemUser, map it to a table and reference it at your Page entity. When you log in using Membership, you could query that SystemUser and store it in HttpSession so you can use it later.

Resources