简体   繁体   中英

select table in database using a parameter in WCF service

I am still a beginner at writing C# and SQL and was wondering if someone could help me with this basic question. I have looked all over the internet and am still stuck.

I am trying to write a WCF service to access my database. I only need one method:

public PathDto GetPath(string src, string trg)
    {
        using (var context = new PathsEntities())
        {
            var p = (
                    from a
                    in context.src
                    where a.Target = trg
                    select a).Distance, Path;
        }
    }

where the parameter src is the table name, and the string trg is the entity's primary key.

Visual studio gives me the error: ... pathsService does not contain a definition for src because it is trying to look up the table "src" and not the string contained in the variable.

How can I use my parameter in the lookup statement?

I am going to assume you are using DbContext EF5.0 stuff

public PathDto GetPath(string tableType, string id)
{
    using (var context = new PathsEntities())
    {
            var type = Type.GetType(tableType);
            var p = context.Set(type).Find(id);
            return (PathDto)p;
    }
}

Seems you DON'T use EF 5.0 and have only got EF 4.0 and are using ObjectContext. Try this...no idea if it works since I don't really use EF 4.0. Alternatively download EF 5.0

public PathDto GetPath(string tableType, string id)
{
    using (var context = new PathsEntities())
    {
            var type = Type.GetType(tableType);
            var p = context.GetObjectByKey(new EntityKey(tableType, "id", id));
            return (PathDto)p;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM