Getting the Schema from an EDMX file - entity-framework-4

I need to modify the T4 template POCO.tt to retrieve the database schema from the EDMX file. I can see the schema stored in an EntitySet tag in the XML. However I cannot find the schema anywhere when using an EntitySet object.
Anyone know where I would find the database schema?
Thanks

UPDATE
I wrote up my findings on this in a blog post:
http://www.ninjanye.co.uk/2011/06/getting-schema-information-from-edmx.html
http://jnye.co/Posts/3/getting-schema-information-from-an-edmx-file-with-poco
I came across this same problem myself.
First you need to retrieve the EntityContainer from the Storage Model Content (edmx:StorageModels) section of the edmx file
At the top of the tt template (after the MetadataLoader is instantiated and the inputFile is declared) add the following code to get the Storage Model Content EntityContainer
StoreItemCollection sic;
loader.TryCreateStoreItemCollection(inputFile, out sic);
EntityContainer sicEntityContainer = sic.GetItems<EntityContainer>().First();
Then from within the foreach (var entity in ItemCollection.GetItems...) loop you can get the current schema with the following
EntitySet eset = sicEntityContainer.GetEntitySetByName(code.Escape(entity), true);
string schemaName = eset.MetadataProperties["Schema"].Value.ToString();
Note: You may have to repeat the get schema code for ComplexType properties lower down in the tt template

I think I misunderstood your question the first time. Have you examined the edmx schema for any clues?
According to this link: http://msdn.microsoft.com/en-us/library/cc982042.aspx
The schema for applications that
target the .NET Framework version 4 is
defined in the
Microsoft.Data.Entity.Design.Edmx_2.xsd
file. The schema for applications that
target the .NET Framework version 3.5
SP1 is defined in the
Microsoft.Data.Entity.Design.Edmx_1.xsd
file.
Those are in %VS100COMNTOOLS%\..\..\Xml\Schemas\ for VS 2010, and %VS90COMNTOOLS%\..\..\Xml\Schemas\ (the 3.5 only) for VS 2008

I'm working with EF6 and wanted to add a summary comment to the classes being generated by the t4 template. After hacking around for a while, I managed to do it by loading the EDMX file and using XPath to find what I needed.
var xmlContent = XDocument.Load(textTransform.Host.ResolvePath(inputFile));
var edmxNavigator = xmlContent.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(edmxNavigator.NameTable);
nsMgr.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2009/11/edmx");
nsMgr.AddNamespace("store", "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator");
nsMgr.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/11/edm/ssdl");
nsMgr.AddNamespace("cs", "http://schemas.microsoft.com/ado/2009/11/mapping/cs");
//This is the loop that came with the default template
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
var mappingAttribute = edmxNavigator.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:Mappings/cs:Mapping/cs:EntityContainerMapping/cs:EntitySetMapping/cs:EntityTypeMapping[#TypeName=\"" + entity.FullName + "\"]/cs:MappingFragment/#StoreEntitySet", nsMgr);
var entitySet = edmxNavigator.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema/ssdl:EntityContainer/ssdl:EntitySet[#Name=\"" + mappingAttribute.Value + "\"]", nsMgr);
var actualTableName = (entitySet.SelectSingleNode("#Table") ?? entitySet.SelectSingleNode("#Name")).Value;
var actualSchemaName = (entitySet.SelectSingleNode("#Schema", nsMgr) ?? entitySet.SelectSingleNode("#store:Schema", nsMgr)).Value;
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
/// <summary>
/// Database Object: <#=actualSchemaName#>.<#=actualTableName#>
/// </summary>
<#=codeStringGenerator.EntityClassOpening(entity)#>

See http://brewdawg.github.io/Tiraggo.Edmx/ you can install it via NuGet within Visual Studio and it serves up ALL of the metadata from your EDMX files that Microsoft hides from you, very simple, works great. You want access to all that low level storage information like your property SQL types, the schema, it's all there. You can even use the Sample Windows.Forms app in the github repo to set a breakpoint and examine the data.

Facing this issue a few weeks ago. As a result create Xslt transformation of EDMX to XSD. https://wash-inside-out.blogspot.com/2022/12/edmx-file-to-xsd-with-xslt.html

Related

How to pass parameter to the table's datasource with Telerik Reporting?

I have a table on my Telerik report where I want to pass parameters with the table's data source. I also have a report datasource with parameter but this does not help with the table's datasource and its parameter. Any help would be appreciated.
this.table1.DataSource = "GetLocationsData";
You have to set the available values source and most probably filters:
Telerik.Reporting.ReportParameter reportParameter1 = new Telerik.Reporting.ReportParameter();
this.csvDataSource1 = new Telerik.Reporting.CsvDataSource();
this.table1 = new Telerik.Reporting.Table();
this.csvDataSource1.Source = "id,text\r\n1,a\r\n2,b\r\n3,c";
this.table1.DataSource = this.csvDataSource1;
this.table1.Filters.Add(new Telerik.Reporting.Filter("= Fields.text", Telerik.Reporting.FilterOperator.Equal, "= Parameters.paramString.Value"));
reportParameter1.AvailableValues.DataSource = this.csvDataSource1;
reportParameter1.AvailableValues.ValueMember = "=Fields.Text";
reportParameter1.Name = "paramString";
reportParameter1.Text = "String";
reportParameter1.Value = "a";
reportParameter1.Visible = true;
this.ReportParameters.Add(reportParameter1);
If this does not help you, you can have a look at the available reports that come with the installation - replace in the path below your Telerik Reporting version, mine is R3 2022. View Code (F7) on the report's designer.cs file:
C:\Program Files (x86)\Progress\Telerik Reporting R3 2022\Examples\CSharp\.NET Framework\ReportLibrary
Another option if you are stuck, is to create the report in the Visual Studio Designer and View the generated code (F7) on the yourreportname.designer.cs file.
If the report was created using the web report or standalone report designer you can import the report definition file (trdp, trdx, trbp) into Visual Studio (will be converted to type definition .cs file) first.
Reference:
https://docs.telerik.com/reporting/designing-reports/connecting-to-data/report-parameters/how-to-add-report-parameters
https://docs.telerik.com/reporting/designing-reports/connecting-to-data/report-parameters/overview

MVC5 with ReportViewerForMvc

I am designing an MVC application using ReportViewerForMVC. This is my controller code:
ReportViewer rp = new ReportViewer();
rp.ProcessingMode = ProcessingMode.Local;
rp.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath)
+ #"Report/sampleFile.rdlc";
ViewBag.ReportViewer = rp;
This is my View:
#using ReportViewerForMvc
#Html.ReportViewer(ViewBag.ReportViewerMicrosoft.Reporting.WebForms.ReportViewer)
The iframe shows but i get this message:
A data source instance has not been supplied for the data source 'DataSet1'.
as my output for the report section. I thought i specified my data source when designing my .rdlc file.
Again, i want to ask if i create a datatable with a where clause having parameter, how can i specify the value in my controller.
I have searched online and i'm not getting any useful. Can anyone please help me out?
The report viewer has no DataSource. If you like using designer view like myself, you could start by creating a dataset and adding a dataAdapter which will automatically add a dataTable. The dataSource can be set like this:
DataSet1 ds = new DataSet1();
TableAdapter1 ta = new TableAdapter1();
ta.Fill(ds.Table[0]);
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet";
rds.Value = ds.Table[0];
rp.LocalReport.DataSources.Clear();
rp.LocalReport.DataSources.Add(rds);
rp.LocalReport.Refresh();
As simple as that...
Change the following in View
#Html.ReportViewer(ViewBag.ReportViewerMicrosoft.Reporting.WebForms.ReportViewer)
to
#Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)

How to use Slickgrid Formatters with MVC

I am working on a first Slickgrid MVC application where the column definition and format is to be stored in a database. I can retrieve the list of columns quite happily and populate them until I ran into the issue with formatting of dates. No problem - for each date (or time) column I can store a formatter name in the database so this can be retrieved as well. I'm using the following code which works ok:
CLOP_ViewColumnsDataContext columnDB = new CLOP_ViewColumnsDataContext();
var results = from u in columnDB.CLOP_VIEW_COLUMNs
select u;
List<dynColumns> newColumns = new List<dynColumns>();
foreach(CLOP_VIEW_COLUMN column in results)
{
newColumns.Add(new dynColumns
{
id = column.COLUMN_NUMBER.ToString(),
name = column.HEADING.Trim(),
field = column.VIEW_FIELD.Trim(),
width = column.WIDTH,
formatter = column.FORMATTER.Trim()
});
}
var gridColumns = new JavaScriptSerializer().Serialize(newColumns);
This is all fine apart from the fomatter. An example of the variable gridColumns is:
[{"id":"1","name":"Date","field":"SCHEDULED_DATE","width":100,"formatter":"Slick.Formatters.Date"},{"id":"2","name":"Carrier","field":"CARRIER","width":50,"formatter":null}]
Which doesn't look too bad however the application the fails with the error Microsoft JScript runtime error: Function expected in the slick.grid.js script
Any help much appreciated - even if there is a better way of doing this!
You are assigning a string to the formatter property, wich is expected to be function.
Try:
window["Slick"]["Formatters"]["Date"];
But i really think you should reconsider doing it this way and instead store your values in the db and define your columns through code.
It will be easier to maintain and is less error prone.
What if you decide to use custom editors and formatters, which you later rename?
Then your code will break or you'll have to rename all entries in the db as well as in code.

EF5 need update ContainerName.FunctionImportName for accessing Stored Procedure when updating models, Any charming solution?

I'm new to entity framework, please forgive me if my question is too simple.
I'm using EF5 build my project at the moment, there is one Function Import "GetStockItem" in my project, which calls a stored procedure and returns data from SP. Every time when I "Update Model from database" from Model Diagram, the update wizard reflects the changes of database without problem, but GetStockItem stops working. The error message when I call GetStockItem is:
"The value of EntityCommand.CommandText is not valid for a StoredProcedure command. The EntityCommand.CommandText value must be of the form 'ContainerName.FunctionImportName'."
The solution, as instructed in the error message is clear, all I need is to add ContainerName. before the FunctionImportName (GetStockItem in my case) in the context.cs file.
My question is how can I avoid the from happening every time when I update models from database? It's quite annoying to do this manual thing now and then, and it's easy to forget to do this then cause users' complaint.
Hope someone can enlighten me with charming solution! Cheers!
I just ran into this using EF5/DbContext. The solution I found was to edit the T4 template ([Model].Context.tt) that generates the DbContext.
In this file, locate the instructions for generating the ExecuteFunction call. For me, it started on line 288:
public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
{
var parameters = _typeMapper.GetParameters(edmFunction);
var returnType = _typeMapper.GetReturnType(edmFunction);
var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
if (includeMergeOption)
{
callParams = ", mergeOption" + callParams;
}
return string.Format(
CultureInfo.InvariantCulture,
"return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
edmFunction.Name,
callParams);
}
Modify the return line so that edmFunction.Name is replaced with edmFunction.FullName and upon saving, the Function Import code will be regenerated using fully-qualified names.
I had a similar issue, I suggest not to change the context.cs file at all; only make sure the connection strings in app.config file generated by EF is the same in the calling project, especially the metadata that in the connection string is very important to be correct. If it helps, please mark this answer accepted otherwise send me the steps to reproduce this error.

Corrupting Access databases when inserting values

Recently, a program that creates an Access db (a requirement of our downstream partner), adds a table with all memo columns, and then inserts a bunch of records stopped working. Oddly, there were no changes in the environment that I could see and nothing in any diffs that could have affected it. Furthermore, this repros on any machine I've tried, whether it has Office or not and if it has Office, whether it's 32- or 64-bit.
The problem is that when you open the db after the program runs, the destination table is empty and instead there's a MSysCompactError table with a bunch of rows.
Here's the distilled code:
var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=corrupt.mdb;Jet OLEDB:Engine Type=5";
// create the db and make a table
var cat = new ADOX.Catalog();
try
{
cat.Create(connectionString);
var tbl = new ADOX.Table();
try
{
tbl.Name = "tbl";
tbl.Columns.Append("a", ADOX.DataTypeEnum.adLongVarWChar);
cat.Tables.Append(tbl);
}
finally
{
Marshal.ReleaseComObject(tbl);
}
}
finally
{
cat.ActiveConnection.Close();
Marshal.ReleaseComObject(cat);
}
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
// insert a value
using (var cmd = new OleDbCommand("INSERT INTO [tbl] VALUES ( 'x' )", connection))
cmd.ExecuteNonQuery();
}
Here are a couple of workarounds I've stumbled into:
If you insert a breakpoint between creating the table and inserting the value (line 28 above), and you open the mdb with Access and close it again, then when the app continues it will not corrupt the database.
Changing the engine type from 5 to 4 (line 1) will create an uncorrupted mdb. You end up with an obsolete mdb version but the table has values and there's no MSysCompactError. Note that I've tried creating a database this way and then upgrading it to 5 programmatically at the end with no luck. I end up with a corrupt db in the newest version.
If you change from memo to text fields by changing the adLongVarWChar on line 13 to adVarWChar, then the database isn't corrupt. You end up with text fields in the db instead of memo, though.
A final note: in my travels, I've seen that MSysCompactError is related to compacting the database, but I'm not doing anything explicit to make the db compact.
Any ideas?
As I replied to HasUp:
According MS support, creation of Jet databases programmatically is deprecated. I ended up checking in an empty model database and then copying it whenever I needed a new one. See http://support.microsoft.com/kb/318559 for more info.

Resources