简体   繁体   中英

Add record to an SQL Azure table using LINQ where ID is “identity (auto_increment)”

I am trying to pass a LINQ query in my Service1.svc.cs to add a new Asset which is an entity in my SQL Azure database. Also AstID is the PK and is set as identity in SQL Azure

I have an OperationContract as follows in my IService1.cs

[OperationContract]
void AddAsset(string AstName, string AstCondition);

and the following void in my Service1.cs

 public void AddAsset(string AstName, string AstCondition)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            Asset AssetObj = new Asset();
            AssetObj.AstName = AstName;
            AssetObj.AstCondition = AstCondition;
            context.Assets.InsertOnSubmit(AssetObj);
            context.SubmitChanges();        
        }

In my Client Application, where i'm consuming the WCF Service, i have the following:

public void AddAssetQuery(string AstName, string AstCondition)
        {
            Service1Client proxy = new Service1Client();
            proxy.AddAssetCompleted += (proxy_AddAssetCompleted);
            proxy.AddAssetAsync(AstName, AstCondition);

        }

and

private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            AddAssetQuery("Dell PC","Mint");

        }

I also have a Completed void, wherein i have a MessageBox that notifies me that the action was completed. Even though, when i check my SQL Azure databse, no data is being fed. Any ideas, why?

Try:

    public void AddAsset(string AstName, string AstCondition)
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        Asset AssetObj = new Asset();
        AssetObj.AstName = AstName;
        AssetObj.AstCondition = AstCondition;
        context.Assets.Add(AssetObj);
        context.SaveChanges();        
    }

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