views:

38

answers:

2

I've been trying to get a WCF service to access a file database that is stored on a local file system to no avail currently.

I created a database called data.mdf in visual studio 2010 and ran this sql query on it.

create table Person 
   (PersonID int identity (1000,1) not null, 
    Name nvarchar(50) not null,
    Address nvarchar(max)

create table ImportantPerson
   (ImportantPersonID int indentity(1000,1) not null, 
    Name nvarchar(50) not null,
    Address nvarchar(max)

and it successfully created the tables I wanted so I proceeded to create my DataContract:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace MyDataContract
{
    public class DataDB: DataContext
    {
        public Table<Person> Persons;
        public Table<Important> Importants;
        public DataDB(string connstr)
            : base(connstr)
        {
        }

    }
 [DataContract]
    [Table(Name = "Person")]
public class Person
{
 private int _ID;
private String _Name;
private String _Address
public Person()
{
}
public Person(String Name, String Address)
{
_Name = Name;
_Address = Address;
}
[DataMember]
[Column(IsPrimaryKey = true, Storage = "PersonID", DbType="int not null", IsDbGenerated = true)]
public int PersonIdentification
{
get{ return _PersonID;}
set{ _PersonID = value;}
}
[DataMember]
[Column(Storage = "Name")]
{
get{ return _Name;}
set{ _Name = value;}
}
[DataMember]
[Column(Storage = "Address")]
{
get{ return _Address;}
set{_Address= value;}
}
public class Person
{
 private int _ID;
private String _Name;
private String _Address
public ImportantPerson()
{
}

public ImportantPerson(String Name, String Address)
{
_Name = Name;
_Address = Address;
}
[DataMember]
[Column(IsPrimaryKey = true, Storage = "ImportantPersonID", DbType="int not null", IsDbGenerated = true)]
public int PersonIdentification
{
get{ return _PersonID;}
set{ _PersonID = value;}
}
[DataMember]
[Column(Storage = "Name")]
{
get{ return _Name;}
set{ _Name = value;}
}
[DataMember]
[Column(Storage = "Address")]
{
get{ return _Address;}
set{_Address= value;}
}

When I try to instantiate a connection to the database

MyDataContract.DataDB data = new MyDataContract.DataDB(@"c:\data\data.mdf");

I get an exception

Bad Storage property: 'PersonID' on member 'MyDataContract.Person.PersonIdentification'.

Can someone help me figure out what's wrong? From everything I've read this should work although I could miss something.

A: 

I'm missing ImportantPerson class in your code with Table attribute pointing to correct DB table and your code contains Person class twice.

Ladislav Mrnka
A: 

Ladislav Mrnka is right but that was caused by a copy paste error. I did solve it on my own, so in case someone has the same problem is will post how to fix it explicitly. To fix it i had to also put the name of the field, and change the Storage attribute like so for : the table person has the "Name" and in the Person Object has the private instance variable "_Name" the property must be set up as:

[DataMember]
[Column(Name="Name",Storage="_Name")]
public String Name
{ 
get{...}
set{...}
}

Also should mention that I only successfully got it to work when the intance variable was private and the property was public.

Will