[ask]asp.net mvc update database error - asp.net-mvc

I have two model classes in one-to-one relationship:
class Person
{
public int PersonID { get; set; }
public int DetailPersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DetailPerson DetailPerson { get; set; }
}
class DetailPerson
{
public int DetailPersonID { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
}
and the code for the edit page view:
#using (Html.BeginForm())
{
#Html.HiddenFor(m => m.PersonID)
#Html.LabelFor(m => m.FirstName)
#Html.TextBoxFor(m => m.FirstName)
#Html.LabelFor(m => m.LastName)
#Html.TextBoxFor(m => m.LastName)
#Html.LabelFor(m => m.DetailPerson.Address)
#Html.TextBoxFor(m => m.DetailPerson.Address)
#Html.LabelFor(m => m.DetailPerson.PhoneNumber)
#Html.TextBoxFor(m => m.DetailPerson.PhoneNumber)
<input type="submit" value="Edit">
}
The EF scaffold uses this code to update data:
db.Entry(person).State = System.Data.EntityState.Modified;
db.saveChanges();
When I submit the edit form, I got an error like this:
A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_dbo.People_dbo.DetailPersons_DetailPersonID ]
But if I do this:
Person p = db.Persons.Find(person.PersonID);
p.DetailPerson = person.DetailPerson;
p.FirstName = person.FirstName;
p.LastName = person.LastName;
db.saveChanges();
update data success without error
I want to know why the first way causse an error,
when I set the breakpoint at the line containing EntityState.Modified,
but the foreign key value ( DetailPersonID ) is 0.
Then, I added #Html.HiddenFor(m => m.DetailPersonID) on the edit form.
I got another error:
A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
I still update database on the other way,
I am just curious why the first way which is EF standart to update data got an error.

You shouldn't use two classes if there is a one-to-one relationship since EF will normalize the db into the corresponding form anyway. Combine your classes like this.
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
}
if you must have separate classes (not recommended), do it like this:
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int DetailPersonID { get; set; }
public virtual DetailPerson DetailPerson { get; set; }
}
public class DetailPerson
{
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public int DetailPersonID { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
}
Then when you retrieve your object, do it like this:
// include the detail when retrieving the parent
Person person = db.People.Include(p=>p.DetailPerson).Single(p=>p.PersonId == whateverIdYou Need);
// modify whatever you like
person.DetailPerson.Address = "my new address";
// then your previous statement will work
db.Entry(person).State = System.Data.EntityState.Modified;
db.saveChanges();

Related

ASP.NET MVC 5 ValidationMessageFor for a foreign key

I have following model classes, using VS 2017, EF and MVC 5.0
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
public class Genre
{
public virtual int GenreId { get; set; }
[Display(Name="Genre Name")]
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
In the View, I have following code
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.AlbumId)
<div class="form-group">
#Html.LabelFor(model => model.GenreId, "GenreId", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("GenreId", String.Empty)
#Html.ValidationMessageFor(model => model.GenreId)
</div>
</div>
When I run the code through VS, the view is displayed with GenreId with a dropdown list box. The dropdown includes a blank value , in addition to the values present in the table Genre.
When I select blank from the dropdown and click "save", an error message is displayed
GenreId field is required
I don't understand where the message is coming from.
In the model class Album, there is no Required annotation for the GenreId property. So how does ASP.NET MVC know to validate the GenreId?
Also, why is a blank value displayed in the dropdown list ?
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
According to your poco class GenreId have to be int value and can not be null.
if you write like this in the below, you generate non-required field.
public virtual Nullable<int> GenreId { get; set; }
Lastly, Razor and MVC can generate default error message if you use or etc.
#Html.ValidationMessageFor()
Note: You use string.empty in the second parameter. This parameter can set default value of Dropdown.
#Html.DropDownList(,string.empty)
Hva a nice coding ;)

Using selected dropdownlist value in query

I am trying to insert data into a sql server temporary table based on a value picked from a dropdownlist. In my model I have 4 classes:
public class Customer
{
[Key]
public int CustID { get; set; }
public string CustName { get; set; }
public virtual ICollection<Sale> Sales { get; set; }
}
}
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
public virtual ICollection<Sale> Sales { get; set; }
}
}
public class Sale
{
[Key]
public int RecordID { get; set; }
public int ProductID { get; set; }
public int CustID { get; set; }
public int InvoiceNo { get; set; }
public int Quantity { get; set; }
public virtual Customer Customer { get; set; }
public virtual Product Product { get; set; }
}
public class TempTable
{
[Key]
public int recordID { get; set; }
public string CustName { get; set; }
public string PoductName { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public int InvoiceNo { get; set; }
}
In my SaleController I have the following action:
public ActionResult OldInvoice(Sale sale)
{
ViewBag.InvoiceNo = new SelectList(db.Sales, "RecordID", "InvoiceNo", sale.RecordID);
return View();
}
[HttpPost]
public ActionResult OldInvoice(TempTable t, int InvoiceNoToSearch = 0)
{
var query = (from s in db.Sales
join c in db.Customers on s.CustID equals c.CustID
join p in db.Products on s.ProductID equals p.ProductID
where s.InvoiceNo.Equals(s.InvoiceNo == InvoiceNoToSearch)
select new
{
s.InvoiceNo,
s.Quantity,
c.CustName,
p.ProductName,
p.Price
}).ToList();
foreach (var temp in query)
{
t.CustName = temp.CustName;
t.InvoiceNo = temp.InvoiceNo;
t.PoductName = temp.ProductName;
t.Price = temp.Price;
t.Quantity = temp.Quantity;
db.TempTables.Add(t);
db.SaveChanges();
}
return RedirectToAction("OldInvoice");
}
and finally my view is:
#model IEnumerable<UsingDropDownListValueInQuery.Models.Sale>
#{
ViewBag.Title = "OldInvoice";
}
<h2>OldInvoice</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
#Html.DropDownList("InvoiceNo")
<p>
<input type="submit" value="Save" />
</p>
}
I get the following error:
Unable to cast the type 'System.Boolean' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
If I enter in the Where Statement an Existing InvoiceNo it does the job but not if I try to pick the value from the dropdownlist
The problem seems to be that you're never actually sending the value back. In your view, you've created a select named "InvoiceNo", but in your post action, you don't have any parameters that can accept that value. The closest and probably the one you intend to bind to is InvoiceNoToSearch, but if that's the variable you want to use, your view needs to match:
#Html.DropDownList("InvoiceNoToSearch")
Of course, that means your SelectList in ViewBag no longer binds automatically as the options for that select, but that can be fixed by just explicitly specifying it:
#Html.DropDownList("InvoiceNoToSearch", (SelectList)ViewBag.InvoiceNo)

populating list in view using editorTemplates

here is my model
namespace chPayroll.Models.CustInformations
{
public class CustContact
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; }
public string cNo2 { get; set; }
public string cNo3 { get; set; }
public List<CustContact> contact { get; set; }
}
}
here is my editorTemplates
#model chPayroll.Models.CustInformations.CustContact
#Html.TextBoxFor(model => model.cNo1)
#Html.TextBoxFor(model => model.cNo2)
#Html.TextBoxFor(model => model.cNo3)
I need to show three textbox for taking email,three text box for taking telephone no. in view. how can I add the items to the list contact defined in the model so that it shows like this
email:--textbox1----textbox2----textbox3--
telephone:--textbox1----textbox2----textbox3--
and sends value to the controller
actually I am trying to send my data in list named contact here ie inside list at
index 0-email1-email2-email3
index 1-tel1-tel2-tel3
#Sanjay: you have a strange construct in your view model:
public class CustContact
{
public List<CustContact> contact;
}
Even if it compiles and machine understands it, I wouldn't use it as it is - you trying to lift yourself from the ground by pulling your hair up :)
It should be defined something along these lines (following your naming conventions & logic):
public class CustContact // single
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; } // those are actual phones, emails etc data
public string cNo2 { get; set; }
public string cNo3 { get; set; }
}
public class CustContacts // plural
{
public List<CustContact> Contacts;
}
View:
#model CustContacts
#EditorFor(m => Model)
Editor template:
#model CustContact
#Html.EditorFor(m => m.cNo1)
#Html.EditorFor(m => m.cNo2)
#Html.EditorFor(m => m.cNo3)
For brevity, we don't deal here with annotations, decorations, error handling etc.
Hope this helps.
Based on the comment on the question, I would build the models as below
public class CustContact
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; }
public string cNo2 { get; set; }
public string cNo3 { get; set; }
}
public class Customer
{
public CustContact Email {get; set;}
public CustContact Telephone {get; set;}
}
then create an editor template for Customer and in that editor template have following logic
#Html.EditorFor(model => model.Email)
#Html.EditorFor(model => model.Telephone)
Hope this helps

Entity framework to object with collection, with Automapper, exception on update

I have an EF 4.1 model, two tables are generated PERSON and ADDRESS from my database.
//This method works
public void Update(IPerson person)
{
var personDb = _dataContext.PERSON.SingleOrDefault(x => x.ID == person.Id);
Mapper.Map<Person, PERSON>((Person)person, personDb);
_dataContext.SaveChanges();
}
But when I remove the .Ignore() in Automapper mapping, I get this exception :
The EntityCollection could not be initialized because the relationship manager for the object to which the EntityCollection belongs is already attached to an ObjectContext. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.
I'd like when I added an new address to the existing addresses save the person and address.
Any idea ?
Thanks,
public void AutomapperInit()
{
Mapper.CreateMap<Person, PERSON>()
.ForMember(x => x.ADDRESS, opt => opt.Ignore());
Mapper.CreateMap<PERSON, Person>()
.ForMember(dest => dest.Address, option => option.MapFrom(src => src.ADDRESS.Select(address => Mapper.Map<ADDRESS, Address>(address)).ToList()));
Mapper.CreateMap<Address, ADDRESS>();
Mapper.CreateMap<ADDRESS, Address>()
.ForMember(dest => dest.Rue, option => option.MapFrom(src => src.STREET));
}
public interface IPerson
{
int Id { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
ICollection<IAddress> Address { get; set; }
}
public interface IAddress
{
string Rue { get; set; }
string Number { get; set; }
int PersonId { get; set; }
}
class Person : IPerson
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<IAddress> Address { get; set; }
}
class Address : IAddress
{
public string Rue { get; set; }
public string Number { get; set; }
public int PersonId { get; set; }
}
Does
var personDb = Mapper.Map<Person, PERSON>((Person)person, personDb);
_dataContext.Attach(personDb);
_dataContext.SaveChanges();
give the results you expect, or am I misinterpreting your question?

Entity Framework, relation between two tables

My model classes are:
public class User
{
public User()
{
Polls = new List<Poll>();
}
public int Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
ICollection<Poll> Polls { get; set; }
}
public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public User User { get; set; }
}
public class PollsContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Poll> Polls { get; set; }
}
EF created a table User_Id column for Polls table.
In the Create view of the Poll, I want to specify the UserId for new poll belongs too, but intellisense shows there is no access to model.UserId, there is a model.User.Id, which is not allowing me create a Poll for existing user and rather creates new User with new Id and no association is created.
<div class="editor-label">
#Html.LabelFor(model => model.User.Id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.User.Id)
#Html.ValidationMessageFor(model => model.User.Id)
</div>
What is the right way of creating a new Poll for an existing User then ?
public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
I think your Poll class should look like:
public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public int User_Id { get; set; }
public User User { get; set; }
}
If you have a column in your Poll table User_Id, then your model should also have this column. You can then just assign a User_Id to a Poll and Entity Framework will take care of the correct linking to the right User object.
Further, consider making your association properties (Poll.User and User.Polls) virtual in order to use the lazy loading mechanisms of Entity Framework.
See this

Resources