I get the following error when I fill out my form in my MVC Application:
UpdateException was unhandled by user code
Unable to update the EntitySet 'Customer' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
My Customer table has: ID (PK), Name, Age, Address and a TimeStamp.
The form only allows Name and Address to be filled out (don't know why - I'm new to MVC, ADO.NET btw)
This is my code:
[HttpPost]
public ActionResult Create(Customer customer)
{
customer.ID = 5;
db.Customers.AddObject(customer);
db.SaveChanges();
return View();
}
I am leaving customer.ID = 5 as a hard coded temp solution for the time being.
If ID is your primary key, Why you are updating that ? You should be updating the rest of the properties.
[HttpPost]
public ActionResult Create(Customer customer)
{
customer.Name= "New name";
db.Customers.AddObject(customer);
db.SaveChanges();
return View();
}
Do you have primary key in your Customer Table ?
This error originates when you do not have primary key on table or when your entity set is mapped with database view.
Related
Actually I'm confused for the case, which relation fits best for my case, and in my opinion the best one is to have a table with 3 primary keys.
To be more specific.
I have a Person model in one of my db's, which has structure like
Person:
Id,
FirstName,
LastName,
...
And the other model Department, which has structure mentioned below
Department:
Id,
Name,
Description,
...
And goal is to set up Editors of schedule for each department and add also admins, whioch will approve requested schedules from editors. Editors and Admins are from same Person table, and if to assume, we need to map some Persons and department with some type.
I'm thinking about to have a mapping table with structure
PersonID,
DepartmentID,
Type (editor or admin)
And not sure, which relation fits best for this. If to have belongsToMany relation here with primary keys PersonID and DepartmentID, we will face an issue, because same Person possibly can be as editor and as admin for one single department. I have MS SQL server as a db.
Any suggestions will be appreciated
you can define many to many relations and use wherePivot method to select by pivot table Type column:
// Department model
public function admins()
{
return $this->belongsToMany(Person::class)->wherePivot('type', 'admin');
}
public function editors()
{
return $this->belongsToMany(Person::class)->wherePivot('type', 'editor');
}
// Person model
public function departmentsWhereIsAdmin()
{
return $this->belongsToMany(Department::class)->wherePivot('type', 'admin');
}
public function departmentsWhereIsEditor()
{
return $this->belongsToMany(Department::class)->wherePivot('type', 'editor');
}
// Note: we use methods names without parentheses
// usage for department
$department = Department::first(); // for example
dump($department->admins);
dump($department->editors);
// usage for person
$person = Person::first(); // for example
dump($person->departmentsWhereIsAdmin);
dump($person->departmentsWhereIsEditor);
My User model code is
public function companies()
{
return $this->belongsToMany('App\Company');
}
My companies table has below field
id
company_name
company_address
company_phone
created_at
updated_at
and My company_user table has below field
user_id
company_id
created_at
updated_a
My Company model has below code
public function users()
{
return $this->belongsToMany('App\User');
}
When I call
$user=Auth::user()->companies;
It shows nothing. But My company has data. also, my company_user table has appropriate data. What is the problem
I have a trigger for insert on Opportunity:
trigger OpportunityInsertTrigger on Opportunity (before insert) {
System.debug('Triggered on insert of opportunity. Updating blockchain...');
for (Opportunity opp : Trigger.new) {
List<DNBCompany__c> retrievedCompany = [SELECT Id, Name, Duns__c FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
DNBCompany__c company = retrievedCompany.get(0);
String duns = (String) company.get('Duns__c');
}
}
I have a custom object called DNBCompany that is linked via a Lookup relation (DNBCompany__c) in Opportunity.
How can I retrieve the DNBCompany associated with opp?
[EDITED to reflect answer below]
You could do something like this :
for (Opportunity opp : Trigger.new)
{
DNBCompany__c retrievedCompany = [SELECT Id, Name FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
}
Since the lookup field will be the Id of the company you want to retrieve, you can use an inline SOQL query to get the associated object back.
Keep in mind, you'll have to reference any additional fields you wish to process on in your SOQL query.
I have an existing database, from which I have built a shell web-app using VS2013 and EF6, but I have run into a few problems.
My database has the following tables, for example:
Table: Customer (Id, First, Last, Date)
Table: Assets (Id, CustID)
Table: Orders (Id, AssetId, CustID)
When the EF created the shell webapp for me (which has awesome) it gave me the following method, for example, to create a new Customer:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="Id, First, Last, Date")] Customer customer)
....
return View(customer)
In my database I have a one Customer to many orders and assets, but I don't understand how to use this relationship during a Create operation, or any other.
My question -- How do I "Create" a new Customer when this operation needs to span multiple tables? For example, when the "Create Customer" form is filled out, you would add one or more Assets and/or Orders.
Should I use a stored procedure to do multiple inserts across three different tables? Or can I make a change to the database that will let me use all the EF magic?
Thanks for your help!
You can use a view model that contains all three classes.
public class CreateCustomerViewModel
{
public Customer Customer { get; set; }
public ICollection<Asset> Assets { get; set; }
public ICollection<Order> Orders { get; set; }
}
Your [HttpGet] action method will pass this view model as the model instead of a Customer.
[HttpGet]
public ActionResult Create()
{
CreateCustomerViewModel model = new CreateCustomerViewModel();
return View(model);
}
Your [HttpPost] action method will take the CreateCustomerViewModel as a parameter:
[HttpPost]
public ActionResult Create(CreateCustomerViewModel model)
{
// Create the Customer with the necessary Assets and Orders and save
}
If you're classes and relationships are configured properly, adding the appropriate Assets and Orders to the navigation properties on your Customer entity should trigger EF to automatically insert the assets and orders into the appropriate tables when you insert the Customer.
In my Silverlight with MVVM project, I'm using Entity Framework. I have one table named Customer, with fields CustomerID, Username, age. I have inserted one row programmatically.
Here the CustomerID field is an auto-incremented one. So how can I get the CustomerID value by passing UserName that was inserted?
Need the LINQ Query to get it from Entity Framework..?
Any Help?
The auto-incremented ID should be set in the object, after you call SubmitChanges. That is, for example, newId here should contain the value:
var customer = new Customer { Username = "test", Age = 100 };
dataContext.InsertOnSubmit(customer);
dataContext.SubmitChanges();
var newId = customer.CustomerID;
If you need to get load it subsequently from the database, then use a simple query:
string name = "test";
var customer = dataContext.Customers.Where(customer => customer.Username == test).FirstOrDefault();
if (customer != null)
{
var newId = customer.CustomerID;
}