I had experience in developing reports(SSRS) using ado.net dataset. Now I am working on an application which is not using ADO.net but entity framework but entity framework does not return datatable or dataset. I want to know
Is there any way to use Collection or custom objects return by entity framework in the reporting services?
Entity framework somehow return datatable or dataset.
Or I should create datatable/dataset from the collection returned by entity framework manually.
For the record, I am getting resultset by executing stored procedure from entity framework.
public void getMyReportData()
{
using (myEntityDataModel v = new myEntityDataModel())
{
var reportQuery = (from r in v.myTable
select new
{
l.ID,
l.LeaveApplicationDate,
l.EmployeeNumber,
l.EmployeeName,
l.StartDate,
l.Col1,
l.Col2,
.......,
.......,
l.Address
}).ToList();
reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource datasource = new ReportDataSource("nameOfReportDataset", reportQuery);
reportViewer1.LocalReport.DataSources.Add(datasource);
Stream rpt = loadEmbededReportDefinition("Report1.rdlc");
reportViewer1.LocalReport.LoadReportDefinition(rpt);
reportViewer1.RefreshReport();
//Another way of setting the reportViewer report source
string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
string reportPath = Path.Combine(exeFolder, #"rdlcReports\Report1.rdlc");
reportViewer1.LocalReport.ReportPath = reportPath;
reportParameter p = new ReportParameter("DeptID", deptID.ToString());
reportViewer1.LocalReport.SetParameters(new[] { p });
}
}
public static Stream loadEmbededReportDefinition(string reportName)
{
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream _reportStream = _assembly.GetManifestResourceStream("ProjectNamespace.rdlcReportsFolder." + reportName);
return _reportStream;
}
Original source: Creating Reports in ASP.Net with Entity Framework
Related
The table has to be created dynamically. The table name is dynamic (is sent to API) and columns are static.
Every time api is called, a new table is been created with different name.
Is it possible to do that in Entity Framework? If so - how?
DB is Posstgress.
Thanks
ADO is not the accepted way. I need to do it with Entity Framework.
I tried to write migration that will be activated just when API is called. But it seems that migration can run only when running first.
If you have a bunch of tables with the same columns and you want to switch between them at runtime, you can use SQL Queries.
var blogs = context.Blogs
.FromSql($"SELECT * FROM {QuoteName(tableName)}")
.ToList();
where QuoteName prevents SQL Injection attacks. This one is for SQL Server:
private static string QuoteName(string identifier)
{
var sb = new StringBuilder(identifier.Length + 3, 1024);
sb.Append('[');
foreach (var c in identifier)
{
if (c == ']')
sb.Append(']');
sb.Append(c);
}
sb.Append(']');
return sb.ToString();
}
I'm working on a MVC4 web app with EF 6. I'm using EF code first approach (on a brand new database). So i have the model classes which i use in EF.
Now i need to create some RDLC Reports. To do this i need to create a Dataset. So how can i create a dataset using my model classes? There are relationships between models classes which i need to carry to the dataset.
My ultimate goal is to design and populate data to my report using my ef models.
Thanks in Adance
Generally EF does not support DataSets. If you want to populate DataSets with data loaded with EF then you've got to provide your own functionality for that. Here I present an example how to populate DataTable object from result obtained with query:
public static class IEnumerableExtensions
{
public static DataTable ToDataTable<TEntity>(this IEnumerable<TEntity> entities)
{
DataTable table = new DataTable();
IEnumerable<PropertyInfo> properties = typeof(TEntity)
.GetProperties()
.Where(p => !p.PropertyType.IsClass || p.PropertyType == typeof(string))
foreach(string propertyName in properties.Select( p => p.Name))
{
table.Columns.Add(propertyName);
}
foreach(object item in entities)
{
List<object> propertiesValues = new List<object>();
foreach (PropertyInfo property in properties)
{
propertiesValues.Add(property.GetValue(item));
}
table.Rows.Add(propertiesValues.ToArray());
}
return table;
}
}
You might use then this extension method as follows:
DataTable table = context.People.ToDataTable();
If you want to implement relationships between tables then the logic you have to do will be more complicated. You should use ChildRelations property of DataTable objects to bind them with relations. Then your DataTable objects you might insert into DataSet.
Simple question - where is dbContext.CreateQuery method in Entity Framework 6 and if the answer is there is not such method my question is what to do to get some data by SQL query to an objectQuery?
Cast context to IObjectContextAdapter and use ObjectContext, e.g.:
using (var context = new AdventureEntities())
{
string eSql = "SELECT VALUE c FROM AdventureEntities.Customer AS c ORDER BY c.LastName";
var query = ((IObjectContextAdapter)context).ObjectContext.CreateQuery<Customer>(eSql);
var customers = query.ToList();
foreach (Customer customer in customers)
{
Console.WriteLine("{0}, {1}", customer.FirstName, customer.LastName);
}
}
How can I make a deep copy of an object graph that has been loaded by Entity Framework Code First with proxies enabled?
I'm using code like this:
static public T DeepCopy<T>(T obj)
{
BinaryFormatter s = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
s.Serialize(ms, obj);
ms.Position = 0;
T t = (T)s.Deserialize(ms);
return t;
}
}
However, the serializer complains correctly that the proxy types are not known types.
UPDATE
Using DataContractSerializer with ProxyContractResolver does not work because my class is decorated with DataContract(IsReference = true).
See
Serialize EF Proxy when POCO has IsReference = true Attribute
I am stating out with entity framework. I have created my ADO.NET Entity Model and mapped the entities to a local SQL CE database file (all done via the wizards). I have created a unit test to test the data access and see how things work. The test executes fine and without any exceptions. However, no new row is generated in the database. Please Help!!!
public void TestCreateRelationshipType()
{
using (var c = new TenderModelEntities())
{
IList<RelationshipType> types = c.RelationshipTypes.ToList<RelationshipType>();
int num1 = types.Count();
RelationshipType type = new RelationshipType();
type.Description = "New Client";
c.AddToRelationshipTypes(type);
c.SaveChanges();
IList<RelationshipType> types2 = c.RelationshipTypes.ToList<RelationshipType>();
int num2 = types2.Count();
Assert.AreEqual(num1 + 1, num2);
}
}
New row is added to the database because you call the SaveChanges() function. When you call this on your datacontext, the changes are passed on to the database.
If you don't want to make any changes to the database, just comment out this section like below
// c.SaveChanges();