Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I try to get column property's value by using entity framework in c#.
My Code:
foreach (var columns in _context.Properties)
{
var SelectedColumnValue = columns.GetType().GetProperties().GetValue(0)GetType();
}
How can i get value of propert(fieldname/column name) in entity framework ?
Any help will be appreciated.
Thanks
I don't know where to start. If _context is of type DbContext, Properties wouldn't have worked unless you have a table named Properties. So _context is probably typeof(DbContext). Then again, Type class does not have a property named Properties but it has the GetProperties() method.
In that case, each columns would be an instance of PropertyInfo. Then you could have used columns.Name to get the name of that property. Those names would have nothing to do with columns, but they would probably be pluralized names of tables.
tl;dr
I'm lost.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would assume it wouldn't be complicated to do the following:
I have a VARCHAR database field that will store a C# property such as "DateTime.Now.Year". I want to pass this value into my ASP.NET application and dynamically return 2017.
How do I read the VARCHAR value as a string and get the C# method to invoke the property?
You could achieve this using System.Reflection, but you really need to store information about the assembly, class and property/field/method you want to invoke.
for example if you stored:
AssemblyPath: "c:\something\someassembly.dll"
ClassFullPath: "SomeAssembly.SomeNameSpace.SomeClass, SomeClass"
MethodName: "someMethodName"
Then in your code you could attempt to load the assembly and instantiate the class:
var assembly = Assembly.Load(assemblyPath);
var clss = Activator.CreateInstance(ClassFullPath);
var method = clss.GetType().GetMethod(MethodName);
var result = method.Invoke();
Now that is a super simplified example, and there are many things to consider like access to the method / property / field etc. (i.e. private vs public .. and static vs instance)
So if you can really reduce your cases and design a set of flags / options you can store in your table, it will be possible.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Working on a legacy application where Data Access repository always returns a data set. Changing to any ORM frameworks is not an option for me at this point, looking for best options to map result set to a CLR type
I know of 2 easy ways to do this.
1 - Use Dapper.NET
const string query = "SELECT * FROM Users";
return connection.Query<User>(query);
With Dapper you don't even have to worry about getting the DataTable, just query the SqlConnection and get your type back.
2 - Use Automapper:
List<User> users = AutoMapper.Mapper.DynamicMap<IDataReader, List<User>>(
sourceDataTable.CreateDataReader());
Automapper can map your DataTable to your type.
Both methods require your class to have property names that match the column names in your source data.
I think they are both available as NuGet packages
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
https://www.youtube.com/watch?v=K1xrlc32Tmw&list=PLJUoF2h8Z-brW94dTZ-ZIOhjFq90_lt5K&index=9
4:25 adds a new object in the lineCollection if product does not exist in lineCollection, but at 24:25 it shows duplicating orders? Did i misunderstood how it works?
https://github.com/jedjad/GitHubVS2013
Because the products in duplicate values are not same objects. They may have same names, quantity etc, but initializing a class with same values does not mean that it is the same object as the one initialized before. They are like 2 different apples with same color and size.
If you say that 2 products are same whenever the names are same, then implement IEquatable<Product> in Product class.
public bool Equals(Product other)
{
return Name == other.Name;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My EF insert method is defined as follows:
public void Add(params T[] items)
How can it be used with ObjectDataSource to insert objects?
According to the documentation, the insert method for an ObjectDataSource is designed to call a method that has parameters for each value of the item being inserted, not the item itself (let alone an array of items).
I would either add an overload to your repository that accepts the value for a single item (and perhaps calls Add), or add a mapper somewhere that maps the values to a new item and calls your Add method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Customer is a table and "Customer" is one of the column in that table, When I create EDMX out of this table, "Customer" field suffixed with 1 (Customer1).
any specific reason?
can we rename column name as "Customer" instead.
Its a limitation of the language, no, you can't rename it Customer. A class cannot contain a property or field with the same name of its containing type. So the EDMX designer adds the 1 at the end.
Why is this? Think about constructors and finalizers, they are special methods that have the same name as the containing type. If you have a property with the same name, the compiler will not be able to resolve the constructor because you can't overload a property with a method.
Either pick a better name for the table (Customers) or the field.