Limit Value of a text field - asp.net-mvc

I am new to MVC
I have an Employee POCO like this
[PetaPoco.TableName("tblEmployee")]
[PetaPoco.PrimaryKey("EmployeeId")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
}
Department Id is actually a foreign key coming from Table tblDepartment. So I want to limit the value of DepartmentId in creating new Employee as the values existing in table tblDepartment(column : Id ).How to do this?
Existing code in Create View
#Html.EditorFor(model => model.DepartmentId)

Why not use DropDownList for Department selection?
#Html.DropDownListFor(m => m.DepartmentID, Model.DepartmentList, string.Empty)
public class Yourclass
{
public SelectList DepartmentList { get; set; }
public Yourclass()
{
FillModel();
}
internal void FillModel()
{
this.DepartmentList = GetDepartmentListFromDb();
}
}

Usually reference integrity is managed at a database level.
Make the relationship on your database between Employees and Departments to enforce reference integrity, so if you try to save an Employee whose department id don't exist on Department table it should return an integrity error.
Capture this error on your saving controller and return it to user.
Also, it will help if you allow your users only to load data from a limited list loaded with existing data on the database, as it is suggested in comments.
However this could not be enough, as a valid value could be deleted from database between the moment it was posted to the list in your form and the moment the form is sent to the controller to save data.

Related

Multiple tables update MVC .net

I am new to MVC and this is my function. There are three tables (Order, OrderNotes, Notes), ID is their primary key. One Order can have many Notes, the table OrderNotes has foreign key OrderID(from Booking table) and NotesID(from Notes table). I want to have a Order Edit page to display individual Order (FirstName, LastName), also display a list of its Notes. Here is my DB structure:
Booking table:
{ID,
FirstName,
LastName
}
BookingNotes table:
{ID,
BookingID,
NotesID
}
Notes table:
{ID,
NoteName,
StatusID
}
So how can I implement the list of Notes since it's from multiple tables? It will be able to Create New Note, Delete existing Note in the list row record, not Edit. Linq used in DB query. Thanks.
It would be a better idea to have only 2 tables:
public class Book
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Navigational properties
public virtual List<Note> Notes { get; set; }
}
public class Note
{
public int ID { get; set; }
public int BookID { get; set; }
public string NoteName { get; set; }
public int StatusID { get; set; }
// Navigational properties
public virtual Book Book { get; set; }
public virtual Status Status { get; set; }
}
A third table is useful when you want to reuse the same Note for a different booking. However i think this is not the case.
So to retrieve data for your context make sure you have the DbSet<Book>
public class ApplicationDbContext : DbContext
{
public virtual DbSet<Book> Bookings { get; set; }
}
In your controller (or better in a repository class):
var BookingID = 10; // this is parameter passed to the function
var myBooking = this.dbContext.Bookings
.Include(p => p.Notes)
.ThenInclude(p => p.Status)
.FirstOrDefault(p => p.ID == BookingID);
Map the retrieved booking to a ViewModel, pass it to the View and you're good to go.

Entity Framework - Modeling multiple roles for same entity

I have a contact class
[Table("Contacts")]
public class Contact
{
public string Id { get; set; }
public string Name { get; set; }
}
I have two classes inheriting from Contact:
[Table("Employees")]
public class Employee : Contact
{
public decimal Salary { get; set; }
}
[Table("Suppliers")]
public class Supplier : Contact
{
public string TIN { get; set; }
}
I am using Entity Framework 6.3 as my ORM.
Can I model something where the same contact can be both Employee and
Supplier with the same unique Id.
Employee emp = new Employee();
emp.Id = "C1";
emp.Name = "Employees";
emp.Salary = 10000;
emp.TrackingState = TrackingState.Added;
Supplier sup = new Supplier();
sup.Id = "C1";
sup.Name = "Employees";
sup.TIN = "ABC";
sup.TrackingState = TrackingState.Added;
When I do:
context.Employee.Add(emp);
context.Supplier.Add(sup);
context.Save();
Obviously, it will not allow me to add the record.
I get an error:
"Violation of PRIMARY KEY constraint 'PK_dbo.Contacts'. Cannot insert duplicate key in object 'dbo.Contacts'. The duplicate key value is (C1).\r\nThe statement has been terminated."
Can I make the Supplier also share the same Id as that of employee and
enable insert / update of employee and supplier?
Thanks
I assume you are not doing any specific inheritance mapping which by default will map this as Table per Hierarchy (TPH) inheritance. With this mapping you get a single Db table that contains the aggregate of all the fields in the base type and the dependent types with a discriminator field to know which type the row should be materialized. This allows for doing a query where you ask for a type.
With TPH each row can only be a single type, so you can not have the same row be both an Employee and Supplier.
There is another type of mapping you can do called Table Per Type (TPT) which will create a table for each type, so in your case 3, one for the shared fields, and one for each dependent type. This should allow for what you are asking about. (YMMV)
However, it seems to be that Employee and Supplier would be used in much different domains so I would suggest that you create your Contact table and relate it to both your employee and supplier.
[Table("Contacts")]
public class Contact
{
public string Id { get; set; }
public string Name { get; set; }
}
[Table("Employees")]
public class Employee
{
public string Id { get; set; }
public string ContactId { get; set; }
public decimal Salary { get; set; }
public Contact Contact { get; set; }
}
[Table("Suppliers")]
public class Supplier
{
public string Id { get; set; }
public string ContactId { get; set; }
public string TIN { get; set; }
public Contact Contact { get; set; }
}
Now you can query for an employee:
db.Employees.Include(e => e.Contact).First();
Or for a Supplier:
db.Employees.Include(e => e.Contact).First();
Which might be cleaner than the inheritence query you would need:
db.Contacts.OfType<Employee>().First();
In both the Has A modeling I show above and the Is A Modeling with TPT you are getting three tables. You just have the FK in what I show rather than the same ID in 3 tables with TPT.
There is also Table Per Class you can look at, which is similar to TPT but you don't get a table for the abstract/parent class, rather you get table for each dependent type with all the fields in it. I don't think this is what you want because it would have duplicate data, however, it does mean less joins.

Cascade Delete with Entity Framework

I have these two models:
public partial class Country
{
public Country()
{
this.Dinners = new HashSet<Dinner>();
}
public int CountryID { get; set; }
public string Name { get; set; }
public virtual ICollection<Dinner> Dinners { get; set; }
}
and
public partial class Dinner
{
public Dinner()
{
this.TRsvps = new HashSet<TRsvp>();
}
public int DinnerID { get; set; }
public System.DateTime EventDate { get; set; }
public string Description { get; set; }
public string HostedBy { get; set; }
public Nullable<int> CountryID { get; set; }
public virtual Country Country { get; set; }
}
I got a bit confused on how Entity Framework will act when a user tries to delete a parent entity (in our case it is the Country entity) that has child records (Dinners).
For example if I have the following code inside my mvc action method:-
public ActionResult DeleteConfirmed(int id)
{
Country country = db.Countries.Find(id);
db.Countries.Remove(country);
db.SaveChanges();
return RedirectToAction("Index");
}
And exception will be raised if I try to remove a country which has dinners, which sounds valid.
I tried modifying my code as follow, by including the Dinners when retrieving the Country object:
Country country = db.Countries.Include(a => a.Dinners).Single(a2 => a2.CountryId = id);
db.Countries.Remove(country);
db.SaveChanges();
return RedirectToAction("Index");
No exception will be raised, so I thought that EF would have deleted the child dinners, but what happens is that it updates the countryID FK inside the Dinners table to be null.... (Cascade Set to Null)
I tried looping over the Dinners collection as follows:
public ActionResult DeleteConfirmed(int id)
{
Country country2 = db.Countries.Find(id) ;
foreach(var d in country2.Dinners)
{
db.Dinners.Remove(d);
}
db.Countries.Remove(country2);
db.SaveChanges();
return RedirectToAction("Index");
}
but this raised the following error:
An exception of type 'System.InvalidOperationException' occurred in
System.Core.dll but was not handled in user code
Additional information: Collection was modified; enumeration operation
may not execute.
I realized that I should explicitly call the .Tolist() on the foreach to get the parent and all its children deleted as follows:
foreach(var d in country2.Dinners.ToList())
Can anyone advice if I getting things wrong, or this is the only way to support cascade on delete using EF ?
Thanks
If you want your deletes to cascade automatically, in your OnModelCreating method, you need to manually enable it:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>().WillCascadeOnDelete(true);
}
You are describing the documented behaviour for cascade delete when the foreign key is nullable:
If a foreign key on the dependent entity is nullable, Code First does
not set cascade delete on the relationship, and when the principal is
deleted the foreign key will be set to null.
If you want it to cascade delete then the relationship is required and the foreign key should not be nullable. Change public Nullable<int> CountryID { get; set; } to public int CountryID { get; set; }
Reference:
http://msdn.microsoft.com/en-us/data/jj591620.aspx#CascadeDelete
Additional info following your comment
You don't have to .Include to get cascade delete to work on required relationships i.e. once you have made the foreign key non-nullable. I am sure of this because that is how my application works.
I think you are observing that Remove marks your entire object graph for removal - whether required or not - in the same way that Add marks the entire graph for insertion. NB - I am not 100% sure of this bit so you should test this before you rely on it.
Further reading here:
using Dbset.Add Versus using EntityState.Added
Why Does Entity Framework Reinsert Existing Objects into My Database?
What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

Entity Framework one-to-one relationships errors on insert

I'm having some troubles getting a set of Entities setup to work correctly. I'm using EF v5 in VS2012 against SQL Server 2008 R2 Express.
Everything seems to be correct with the generated database but I'm not having any success writing to some of the tables.
I have a UserProfile object defined like:
[Table("UserProfile")]
public class UserProfile
{
[Key]
public long UserId { get; set; }
[StringLength(56)]
public string UserName { get; set; }
public Country Country { get; set; }
...
}
I also have the Country entity defined like:
[Table("Country")]
public class Country
{
[Key]
public int Id { get; set; }
[StringLength(2)]
public string CountryCode { get; set; }
[StringLength(100)]
public string CountryName { get; set; }
public ICollection<UserProfile> UserProfiles { get; set; }
...
}
The generated columns look correct in the database, if I script it out it looks good:
ALTER TABLE [dbo].[UserProfile] WITH CHECK ADD CONSTRAINT [UserProfile_Country] FOREIGN KEY([Country_Id])
REFERENCES [dbo].[Country] ([Id])
GO
Just for testing purposes in a controller action I have the following:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
{
Country = new Country { Id = 223, CountryCode = "UK", CountryName = "UK" },
...
});
When this WebSecurity method is executed I get the following error:
No mapping exists from object type MyApi.Data.Entities.Country to a known managed provider native type.
I tried setting up a configuration file to specify the relationship in code as well as the database but it's still not playing friendly.
The config looks like:
public class CountryEntityConfig : EntityTypeConfiguration<Country>
{
public CountryEntityConfig()
{
this.HasMany(x => x.UserProfiles)
.WithRequired()
.HasForeignKey(FKey => FKey.Country);
}
}
It feels really odd having a list of profiles in the country object am I getting some of the basics completely wrong with this? Any input appreciated.
EDIT:
I've revisted some classes and made a few changes, in the user profile I now have:
public int CountryId { get; set; }
public virtual Country Country { get; set; }
Country.cs
public class Country
{
public int CountryId { get; set; }
[StringLength(2)]
public string CountryCode { get; set; }
[StringLength(100)]
public string CountryName { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
And the database now contains two CountryId related fields in the UserProfile table with relationships to the Country table and I still get the original error message.
TIA,
The No mapping error is probably because you did not add the Country entity to the DbContext.
In the AccountModels.cs file, you will find a UsersContext that derives from DbContext, in there you must add a DbSet<Country>.
There are, however, other issues here. For instance, you are creating your Country table with a Key, and EF by default will give the Country table an autogenerated identity column. This means you can't insert a value for the ID, it will be auto generated.
Country is probably a lookup table anyways that you will populate with country values. So you would probably just set CountryId to the value of the country you want

MVC MultiSelect modelbinding

i want to be able to display and update my User's Organisations preferably using the htmlhelper Html.TextBoxFor(
I have an entityframework 5 database first database with relationships defined as expected on the 3 tables
User
Organisation
UserOrganisation
which yield the classes below
public partial class User
{
public System.Guid UserId { get; set; }
public string Fullname { get; set; }
...
}
public partial class Organisation
{
public int OrganisationID { get; set; }
public string Title { get; set; }
...
}
public partial class UserOrganisation
{
public System.Guid UserId { get; set; }
public int OrganisationID { get; set; }
}
I pass in the user as the model and also populate a list of potential organisations in the viewbag i.e.
ViewBag.PossibleOrganisations = OrganisationFactories.GetOrganisations()
and the razor markup is.
#Html.ListBoxFor(model => model.UserOrganisations,
new MultiSelectList(ViewBag.PossibleOrganisations,"OrganisationID","Title"))
Now this displays the list of Organisations correctly and i can multiselect them. But it doesn't show the selected Organisations, and it also wont write this back to the database when posting back (incidentally all other fields did write back prior to this change).
Does anyone have any suggestions or examples of a multiselect list working in this fashion?
Cheers
Tim

Resources