I am using Entity Framework with asp.net mvc, but I don't think mvc plays a big role here.
I have an object Customer, and a lookup table (there are several, and they all behave the same way; so for simplicity I'll pick Territory). Both Customer and Territory have LastUpdated field (Datetime that is set manually in the code).
If I hardcode Territory, and get only Customer data from the View, I don't have any problems:
public ActionResult CreateCustomer([Bind(Exclude = "CustId")] Customer cm) {
cm.Territory = (from t in repo.Territory where t.ID == 2 select t).First();
repo.AddToCustomer(cm);
repo.SaveChanges();
}
As I said, no problems. However, if I use a dropdown in the view with the matching id (Territory.ID) - there is a problem. I have the following line in controller:
ViewData["territory"] = new SelectList(repo.Territory, "ID", "Name", 1);
and corresponding line in the View:
Territory: <%= Html.DropDownList("Territory.ID", (IEnumerable<SelectListItem>)ViewData["territory"])%>
I get good news and bad news: the good news is that I get the territory ID nicely assigned to the appropriate member of Customer object. The bad news is that Territory.LastUpdated value is set to 1/01/0001. Obviously, I don't care about this value - but it looks like EF cares. When I call SaveChanges I am getting the following error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
Looks like EF is trying to retrieve the value from the database, and then compare it with EF value... or maybe something else - but the bottom line is I can't figure out how to prevent it from trying to be so smart.
For now, I name DropDown ID to be something different ("terID" instead of "Territory.ID"), and using FormCollection:
int terID = Int32.Parse(formData["terID"]);
cm.Territory = (from d in repo.Territory
where d.ID == terID select d).First();
This works (which makes me comfortable with my analysis) but this cannot be the best way.
I also can't believe that nobody bumped into such problem - but I couldn't find anything relevant... The best I could find is link text but it's more of a hint without much details
I tried to cut out all unrelated stuff from the code - so if there are some typos, they are in the post; not necessarily in the code itself!
Thanks
Territory.LastUpdated is being set to the default value, as EF saves the whole row / what has changed you get an exception.
The easy way to fix this is to set it to DateTime.Now before saving.
I'm assuming you are using EF 1.0 which is not good at direct foreign key maping.
Territory posted from view to action is not bound to Datacontext, hence when you save your customer object - EF saves attached Territory Object as well.
You have to get Territory object from db first and then assign it to customer.
And Your solution is perfectly fine, cos there is no other for EF 1.0 )-:
Related
I created a new field in LedgerJournalTrans(General Ledger > General Journals menu), but click on Post but was unable to get my new field value in the CustTrans, is there any method i need to update to post my new field value to the CustTrans?.
Your question is not clear, but I think I can guess what you're trying to do? You've added a field to the table LedgerJournalTrans and when you post, you're expecting that data to carry over to the CustTrans table? You should work on asking clearer questions, but if my interpretation is correct, then you need to add code so that the posting process does something with that field.
Likely you'll need to modify \Classes\LedgerJournalTransUpdateCust, specifically you'll want to look at methods postNewCustomerVoucher and updateNow.
Find where \Data Dictionary\Tables\LedgerJournalTrans\Fields\CustTransId gets set, as that's the relation between the two tables, and try to write code that follows that Microsoft is doing.
The problem related to using table buffers in AX 2012 with Grid Controls, where first time additions to the form's tempDB were not displayed in real time (but were persistent and subsequent additions worked fine thereafter).
I resolved the problem with help from DAX legend Martin Dráb and Brandon Weise on the Dynamics Community MSDN but I'm posting on SO in case it helps others (as I couldn't find anything close), and I don’t think it hurts the community to add more Dynamics AX content on SO. There are also some learnings to be had about how physical tables link to tempDBs and their relationship to the form's datasource.
Original thread: https://community.dynamics.com/ax/f/33/t/225120
Problem:
I have a Wizard that generates a new form at runtime, containing a Grid Control.
The Wizard passes a reference to one of its temp Tables to the form, in which I use linkPhysicalTableInstance in the runtime form's datasource init() method.
The GridControl has an add new record button, which inserts records in to the tmp table reference.
The new record is saved to the reference temp table correctly, and displays in the grid when the runtime form is closed and reopened but does not display in the grid immediately after the insert.
To add to the weirdness, after a runtime form has been created, a record inserted and then closed, subsequent run time forms do display new record insertions immediately, without needing to be re-opened. Some code snippets below.
Why does this behavior only happen for the first time that data is inserted in to the temp table, but displays fine for subsequent runs of the runtime form?
Creating the runtime form:
args = new Args(formstr(RunTimeFormName));
formRun = classFactory.formRunClass(args);
formRun .parmRuntimeFormsGridTmpDS(sysWizard.ReferenceToWizardsTableTmp()); // Passing a reference for Wizards tmpTable to form
formRun .init();
formRun .run();
formRun .wait();
formRun .detach();
RunTime Form's parmDataSourceMethod:
public void parmRuntimeFormsGridTmpDS(CommentsGridTmp _ReferenceToWizardsTableTmp)
{
ReferenceToWizardsTableTmp = _ReferenceToWizardsTableTmp;
}
DataSource init() method:
public void init()
{
super();
RuntimeFormsGridTmpDS.linkPhysicalTableInstance(ReferenceToWizardsTableTmp);
}
RunTime Form's New button clicked method:
void clicked()
{
int64 numRows;
;
// Refresh records loaded in grid DS to ensure correct number of records for setting initial index number
// Note: SomeId is passed in Args() record, its passing fine as is the number of rows count - and replacing with a static value has no effect.
select count(RecId) from ReferenceToWizardsTableTmp
where ReferenceToWizardsTableTmp.SomeId == someId;
numRows = ReferenceToWizardsTableTmp.RecId;
ReferenceToWizardsTableTmp.Comment = "Comment " + int642str(numRows + 1);
ReferenceToWizardsTableTmp.Filename = "";
ReferenceToWizardsTableTmp.someId = someId;
ReferenceToWizardsTableTmp.insert();
element .Task(#TaskF5);
// super();
}
So as described above, the first time that the runtime form is created and a record is inserted, it doesn't display. Reopening the form will display the inserted data fine. Also, after reopening the form any new records inserted appear in the grid in real time.
I originally supposed it had to be something to do with the linkToPhysicalTable and where the grid fields look for records to display…
By the way, if you have a better answer or explanation then please feel free to contribute.
Solution
I have a working solution whereby I run a select statement on the buffer reference prior to the linkPhysicalTableInstance operation (a delete_from tmptable statement has the same effect and is cheaper), which acts to initialize the buffer reference despite it being empty.
The linkPhysicalTableInstance operation then succeeds at the first run because the buffer properly exists - and changes written to the form DS are now persistent and reflected in the calling Wizard's buffer reference.
In Addition (from Brandon Weise):
In case you happen to be jumping tiers in your code, here's a small gotcha to watch out for.
https://community.dynamics.com/ax/b/dynamicsaxexperience/archive/2016/01/24/2012-unexpected-degeneration-of-insert-recordset-into-tempdb-buffer
Techniques That I Found Useful for Investigation
(Credit to Brandon Weise and Martin Dráb for these)
It does seem that using .linkPhysicalTableInstance(..) to change the underlying temp table associated with a form datasource after it
has already initialized produces some weird behavior. This seems to
be true even when you can demonstrate with .getPhysicalTableName()
that they are linked properly.
One technique to help is to create your form, and
call .init(), but not yet .run(). Then use
.linkPhysicalTableInstance() to link the freshly created temp table
underlying the data source to your external temp table buffer. In
other words, instead of trying to transplant your already created
temp table into the form's data source, let the form create the temp
table, then let the caller transplant that temp table into its own
buffer using .linkPhysicalTableInstance(). Then insert records,
then call .run(). If necessary, call .executeQuery() on the form
data source after .run().
I took a scattergun approach to printing
the table names throughout initialization and operation, and while
the two tables do eventually link correctly with the same table
name, they take a roundabout way of getting there.
Inspect the contents of a temp table while debugging, from SQL Server Management Studio using:
set transaction isolation level read uncommitted;
select * from tempdb..t107946_BE044A13A9C24283897CA1B59607CBD2;
Which is easy if you have the table name from
.getPhysicalTableName(), but even if you don't know it precisely,
it's often easy to locate with a little trial and error. select *
from tempdb.sys.tables; The table will of course start with "t" and
the table number. Often it's the most recently created one, so
sorting by create_date desc floats it to the top, but of course
there can be a pool of them.
Review Methods on a Form Data Source which methods you can use when working with records through a datasource.
I am currently working on a website where we can track small event for our clients. I am using linq-to-sql and asp.net to build it. So far the site is really small about 10 pages, and only 2 tables, events and clients. I have listed the columns for each table below:
Events:
-eventid
-eventname
-datecreated
-details
-datedue
-status
-clientname (this should be clientid)
-sentemail
Clients:
-clientid
-clientname
-clientemail
-clientphone
the issue I am having is getting the 2 database tables* to work together. Ideally I would like to have clientname replaced with clientid in the Events table.
I had it set up this way before but ran into an issue when listing out the events:
When I am listing out the events I would like it to show the client name as appose to the clientID. Unfortunately I am pretty new to ASP.net and although I could easily do it in PHP have no idea how to get it to work. Its not a huge deal but I would like to be able to update the client list and events apart from one another.
Sorry If this makes little to no sense, If there is any confusion I will try to word it better.
*correction on my part
Once you've got your database normalized, create a Linq2Sql context, and then you can write a query similar to this (to get a list of event names and their client names):
var results = from e in dbContext.Events join c in dbContext.Clients on e.clientid equals c.clientid select new { e.eventname, c.clientname };
in addition to the answer of Andrew Lewis you need to:
1) add the clientid field to the events table
2) run a sql query to fill the clientid field:
update events set clientid = (select clientid from client where name = events.clientname)
3) remove the clientname field from the events table
G'day,
OK, I have now rewritten this question totally:
I am trying to import data into Dynamics through the use of the Business Connector (ideally, I would be importing it directly through SQL but I understand that is not a good idea - however I am open to other suggestions). This is to import invoices from a production system into Dynamics / Axapta (v5).
I can code to insert data into the CUSTINVOICETABLE table, which works fine and generates the RECID. However, new invoices just inserted exist without an Invoice ID (until they are posted I understand). However, I need to insert line items into the CUSTINVOICETRANS table as children of the above entry. For this you need to set the INVOICEID field to refer the above as the link to the parent. However, this does not appear possible before the invoice has been posted. Or I may be way off track?
Does anyone have any ideas or can shed any light for me? That would be much appreciated.
Regards,
Steve
To post a "Free text invoice" simply call custPostInvoiceJob.run() method.
You will have have to make the object first, then call a method with your newly created CustInvoiceTable record.
In X++:
custPostInvoiceJob = new CustPostInvoiceJob();
custPostInvoiceJob.updateQueryBuild(custInvoiceTable);
custPostInvoiceJob.run();
You will have to translate that into Business Connector calls in your preferred language.
Ok, it's actually as easy as it should be.
After the insert statement, simply use the get_Field call:
axRecord.Insert();
recID = (long)axRecord.get_Field("RECID");
You insert the line items in the CUSTINVOICELINE table (which uses a PARENTRECID), then upon posting the items get inserted into the CUSTINVOICETRANS table linked to the appropriate invoice number.
I hope this saves someone from having to work this out themselves.
Steve
This is a follow-up to the discussion in this post. I am not sure how to ask the follow-up question in a 2-month-old thread; so I apologize if there is a better way.
Anyway, to reiterate I have an ASP.NET MVC Edit form, and one of the fields is a select list. I get the values fine, but I am having trouble to update the primary entity after Post. Obviously, I have the key to the lookup entity, but it seems crazy to have to load all lookups. So, the suggested solution is entity reference
For clarity, let's say I have a customer as main entity, and title (Mr / Mrs / Dr, etc.) as the lookup.
So, the link above suggests the following:
customer.TitleReference.EntityKey = new EntityKey("MyEntities.Titles", "Id",
Int32.Parse(formData["personTitle"]);
So far, so good. I assign the entity key (and I see in the debugger that is indeed what I expect). But I can't figure out how to get the new value saved along with other customer fields. I am doing the following:
var originalCustomer = (from c in MyEntities.Customers
where c.Id = customer.Id select c).first();
MyEntities.ApplyPropertyChanges(originalCustomer.EntityKey.EntitySetName,
customer);
This updates all customer fields, except for lookups. Intuitively, it is (somewhat) understandable, since if I specify originalCustomer.EntityKey.EntitySetName, ApplyPropertyChanges ignores originalCustomer.TitleReference.EntityKey.EntitySetName.
But if I do specify originalCustomer.TitleReference.EntityKey.EntitySetName, runtime complains that the entity is null (which is also understandable, since I didn't assign anything to the entity; only to entity reference.
As is probably obvious, I am going circles around what seems to be quite straightforward situation. However, I can't find any tutorials that cover it (which is strange in itself).
Furthermore, I have a more complex problem... the customer may have multiple addresses and the address has state... hopefully, once I figure out the titles - I can extrapolate.
By the way, the example (customer - title - address) is fictitious; but it models the problem quite well.
This should work:
var originalCustomer = (from c in MyEntities.Customers
where c.Id = customer.Id select c).First();
originalCustomer.TitleReference.EntityKey = new EntityKey("MyEntities.Titles", "Id",
Int32.Parse(formData["personTitle"]);
context.SaveChanges()