I am working on a project, which uses a custom ORM. Each model is a simple POCO which is populated from the database.
Now, I need to use some of these classes to create a master-detail report in Stimulsoft Reports, and I am following this guide. But instead of using ADO.NET entity model, I use our own ORM.
The problem is, at step 5 of the article, Stimulsoft does not recognize my business objects in the Child of Business Object box and shows an empty list.
I think, the problem is with the relations between my models. Here is an example of a relation in our ORM:
public class Group
{
public int Id;
// Other properties
public List<Person> People { get; set; }
}
public class Person
{
public int Id;
// Other properties
public int GroupId;
}
This is too old instruction. Try to add two DataBand with your Business Objects.
Related
I'm using Rethink DB with C# via RthinkDB.Driver https://github.com/bchavez/RethinkDb.Driver.
I know that in relational databases there is a feature to create references in one document to another. In mongodb it is ObjectID, in LiteDB it is BsonRef attribute or Dbref function.
LiteDB example:
public class Order
{
public int OrderId { get; set; }
[BsonRef("customers")] // where "customers" are Customer collection name
public Customer Customer { get; set; }
}
The question is, how i can declare reference of object to another table?
I read this article https://rethinkdb.com/docs/data-modeling/#linking-documents-in-multiple-tables but there are no examples how to insert documents with references.
The c# driver don't expose annotations for your classes. RethinkDB is only storing json documents that can be queried with relations. Consider writting a little ORM, or comment your classes attributes.
I am using the Repository pattern with.NETCORE and am trying to return data back from an HttpGet request. The data I want back is from multiple un-related tables in SQL. I am trying to wrap my head around being able to retrieve the data from each respective repository and return an object with all data. I hope this makes sense, I am stuck and started to go down "Unit of Work" but can't find a good example that does what I would like, query multiples in un-related tables from one get request. Thanks in advance.
Step 1. Create model classes (domain model layer) for each table / view or the dataset that comes from the SQL database. Shown below:
public class DataFromTable1
{
// properties mapped with the sql table columns
}
public class DataFromTable2
{
// properties mapped with the sql table columns
}
public class DataFromTable3
{
// properties mapped with the sql table columns
}
Step 2. Write data access classes that will call the SQL database (stored proc or direct SQLstatement – I don’t recommend direct table access from .net though) to populate your model classes created in step 1. You can use any repository pattern in this step.
Step 3. Create a view model class that will wrap the model classes with its properties and hydrate them by calling the data access class create in step 2. Generally view model classes are created under the MVC project.
public class MyViewModelClass
{
public DataFromTable1 DataFromTable1 { get; set; }
public DataFromTable2 DataFromTable2 { get; set; }
public DataFromTable3 DataFromTable3 { get; set; }
}
Step 4. Use this view model to display the data in the view.
Hope this helps.
This question already has answers here:
Using DataAnnotations with Entity Framework
(2 answers)
Closed 6 years ago.
I am using EF6 in a database first context. In this case I am using the entity classes in my MVC web project and would like to annotate the entity's fields with various validation and display attributes. However, when I refresh the entity classes by doing an update from database in my edmx designer, the classes regenerate and my attributes are lost.
What is the best approach to getting round this?
When working with generated entity classes in a database first Entity Framework project, it is often necessary to apply attributes to the class’s fields. This is especially the case if you are foregoing the use of ViewModels and using your entities directly in an MVC web project.
Of course if you were to apply validation or display name attributes to the fields directly, the next time the data model is generated due to an upgrade from database action, these would all be overwritten.
Luckily the classes generated by Entity Framework are marked as partial. This means that we can create a second class that augments the first. Effectively the two classes are seen as one. For example:
[MetadataType(typeof(AnimalMetaData))]
public partial class Animal
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int NumberOfLegs { get; set; } etc..
public class AnimalMetaData
{
[MaxLength(200)]
[Required]
public string Name { get; set; }
[MaxLength(1000)]
public string Description { get; set; } etc...
But of course we have a problem here. We have put the attribute to associate the metadata class on the entity class and this will be overwritten on an update from the database. So how do we get round this? Simple! We create a third ‘dummy’ class called Animal that sits alongside the metadata class and is also partial:
[MetadataType(typeof(AnimalMetaData))]
public partial class Animal {}
We annotate this class with our metadata class, so in effect we have the three classes acting as one; the entity itself, the dummy class to link the entity with the metadata definition class.
For the sake of keeping things tidy, you could do worse than to place the metadata and dummy classes together in a separate folder adjacent to the entities generated by Entity Framework.
I want to have a page where the user selects from a drop down list the category, then adds a small text about that category and uploads an image where the path of that image is saved in the database rather than the whole image. I have created a table "Categories" where the admin is authorized to fill it and the user only selects from the categories list.
Here is what I have done so far:
The create categories model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace DemoIdentity.Models
{
public class CategoriesAdmin
{
public int ID { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "category name")]
public string categoryName { get; set; }
}
public class DefaultConnection:DbContext
{
public DbSet<CategoriesAdmin> categories { get; set; }
}
}
Now I want to have another table (Data) which includes (ID, Category (category name selected from table categories), News, Image_Path). This table is in the Default Connection database. The category name is the selected category name from a drop down list, and the image path is an upload image which saves the path rather than the whole image.
I am unsure of how to achieve this.
It appears that you are confusing components of ASP.NET MVC and Entity Framework.
As the Entity Framework site states:
Entity Framework (EF) is an object-relational mapper that enables .NET
developers to work with relational data using domain-specific objects.
It eliminates the need for most of the data-access code that
developers usually need to write.
And the MVC site states that:
The ASP.NET MVC is an open source web application framework that
implements the model–view–controller (MVC) pattern.
The two frameworks meet through your model classes. MVC uses the model class to define the data, logic and rules of the application. In Entity Framework, your model class is mapped to tables in your database where it handles the direct reads and writes for you.
By creating your CategoriesAdmin model class and exposing it as a property in your DbContext class as such:
public class DefaultConnection:DbContext
{
public DbSet<CategoriesAdmin> categories { get; set; }
}
Entity Framework will have mapped your model class to a database table called CategoriesAdmins. If this table does not yet exist in your database, it will automatically create it for you. This approach in Entity Framework is known as Code First to a new Database.
Now since you already have a table that stores the available categories (CategoriesAdmin), you will need to create a second model class (called Data for the sake your example) which contains properties for the other bits of information that you want to store.
public class Data
{
// gets or sets the ID of this Data record.
public int ID {get;set;}
public string ImagePath {get;set;}
// other properties
...
}
Now that you have two model classes, you need to create a relationship between the two. In a SQL database this is achieved by Foreign Keys. In Entity Framework, you can achieve the same by using Navigational Properties.
So we update the Data model class as such:
public class Data
{
// gets or sets the ID of this Data record.
public int ID {get;set;}
public string ImagePath {get;set;}
// gets or sets the ID of the related CategoriesAdmin record.
public int CategoriesAdminId {get;set;}
// gets or sets the related CategoriesAdmin record. Entity Framework will
// automatically populate this property with an object for the related
// CategoriesAdmin record.
[ForeignKey("CategoriesAdminId")]
public virtual CategoriesAdmin CategoriesAdmin {get;set;}
// other properties
...
}
The ForeignKeyAttribute on the CategoriesAdmin property is there to give Entity Framework a further hint of the foreign key column to load the navigational property from.
Finally to be able to use your new Data model class with Entity Framework, you need to add another property to your DbContext class so that you have a means of accessing your data:
public class DefaultConnection:DbContext
{
public DbSet<CategoriesAdmin> Categories { get; set; }
public DbSet<Data> Data { get; set; }
}
Now that you have created your model classes and wired them into Entity Framework, you will now be able to use them in MVC. If you load your Data model into your view (using DefaultConnection.Data), you will be able to access the related CategoriesAdmin record by accessing the CategoriesAdmin property on the Data object.
In short: two tables means you need two models. Both models can be loaded into the single view.
Footnote: Apologies if there are large gaps in my answer as there is a lot to explain that have already been explained in other places far better than what I can. The references I have linked should hopefully fill in the gaps.
Should you need more help, please see all of the tutorials on the ASP.NET MVC website on working with data. They're much better written than my concise attempt. I would recommend following them exactly and getting the examples to work before completing your own project so that you have a better understanding of how the two frameworks work and interact with each other.
folks, i am new to C# DataEntity Framework.
I have 2 tables in DB :
Vehicle with fields id, measurementId.
Measurement with fields Id, Name.
They related as one to one. One vehicle have one measure.
I want to expand entity Vehicles where i want to store MeasurementName field. I've created property MeasurementName, but how i can bind it to Measurement.Name. Is it possible in DataEntity framework ?
I know that i can achive it another way, for example using Entity Linq where i will create new
class Test
{
Id= id,
Measurement = measurement.Name
};
But is it possible to expand DataEntity to have this property ?
Each of the entities in your model is implemented as a partial class, meaning the total code that defines the class can exist in multiple files but gets compiled into one object. This makes them highly extensible as you can add properties and functions to the code generated by the Entity Framework designer. Create a new partial class with the same name as your entity in the same namespace and add the custom property like so
public partial class Vehicles
{
public string MeasurementName {
get {
return this.Measurement.Name;
}
set {
this.Measurement.Name = value;
}
}
}
If this answer was helpful to you, please be certain to vote it up or mark it as the accepted answer.