简体   繁体   中英

Asp.net MVC, validation of unique rows with weak entities

So I have an entity EmployeeRegion

EmployeeRegion is a weak entity with a composite key from the tables Region and Employee .

I have an employee profile where they can add themselves to a region. They get a drop down with the regions

在此处输入图片说明

I'm doing a pass where I get everything in the model compliant with the data validation stuff that's built in to Asp.net MVC. This is great because the validation (using annotations) shows really great error messages to the end user.

How, using annotations, can I validate that a composite key is unique, and if not, show an error message?

Basically, you just need:

public class EmployeeRegion
{
    [Key, Column(Order = 1)]
    public int EmployeeId { get; set; }
    public virtual Employee Employee { get; set; }

    [Key, Column(Order = 2)]
    public int RegionId { get; set; }
    public virtual Region Region { get; set; }
}

In other words, the part you're missing is [Key, Column(Order = N)] . This makes the ids an actual composite key, which out of the box won't allow duplicates.

However, all this does is make the API more difficult for working with this M2M relationship. If the only data in the table is the keys and there's no payload of additional data needed for the relationship, then you should get rid of this entity and simply let EF handle the relationship:

public class Employee
{
    ...

    public virtual ICollection<Region> Regions { get; set; }
}

public class Region
{
    ...

    public virtual ICollection<Employee> Employees { get; set; }
}

Behind the scenes, EF will create a table similar to what you have with EmployeeRegion , but you won't be responsible for managing that, and things like ensuring unique relationships will be baked in. This also buys you a much easier API to work with. For example. To get all the employees for a region, currently, you'd have to do something like:

dbo.EmployeeRegions.Where(m => m.Region.Id == regionId).Select(m => m.Employee)

Whereas, by allowing EF to handle it, you can just do:

region.Employees

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM