开发者

EF4: Get EntitySet specified as string

开发者 https://www.devze.com 2023-01-29 06:01 出处:网络
I have a database with many single isolated tables, and I need to fill their contents to their Entities.

I have a database with many single isolated tables, and I need to fill their contents to their Entities.

Currently I have for each and every tabl开发者_开发技巧e something like this:

try
{
    using(DBContext context = new DBContext())
    {
        var vehicleTypes = context.VehicleTypes;
        return vehicleTypes;
    }
}
catch(Exception ex)
{
//handle error
}

What I'm seeking for is best described as something like this:

var vehicleTypes = context.GetEntitySet(VehicleEntity);
var buildingTypes = context.GetEntitySet(BuildingEntity);

where VehicleEntity, BuildingEntity (...) are entities from entity model. I know I don't have this option explicitly, but something in that similar way would be nice. Extension method is also an option...

I'm working with EntityFramework 4.0, POCO Self Tracking Entities (without proxy).

Thanks

edit: My latest try was this:

    public static IEnumerable<TEntity> GetTableContent<TEntity>() where TEntity:class
    {
        try
        {
            using (var context = new DBEntities())
            {
                var result = context.ExecuteStoreQuery<TEntity>("SELECT * FROM " + typeof(TEntity).Name); //the table names correspond to Entity type class names
                return result;
            }
        }
        catch (Exception ex)
        {
            //handle error...
        }
    }

but I get the error:

The data reader is incompatible with the specified 'DBEntities.MyEntity'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.

and that's true - the names don't match - but I figured it would've mapped the table from query based on edmx definition? How can I get this right?


Start with code which actually works before trying to make it "generic"

result = context.ExecuteStoreQuery<Something>("SELECT SOMETHING_ID AS ID, ..., FROM SOMETHING...");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号