简体   繁体   中英

Entity Framework not writing to Database

I'm trying to take a JSON response, deserialize it, and store in a localdb. Everything is working, but its not writing the data to my database. I can't seem to figure out why its not as I'm not getting any errors.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using DataFetcher.DataFieldsInClasses;
using DataFetcher.EF_Models;
using Newtonsoft.Json;


namespace DataFetcher
{
    class Program
    {
        static void Main(string[] args)
        {
            var teamNames = new TeamsList();
            teamNames.teamList.Sort();

            for (int i = 0; i < 30; i++)
            {
                var cities = teamNames.teamList.ElementAt(i);
                //URL for each indivual team
                string URL = @"http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/" + cities + "/iphone/clubroster.json";
                WebRequest wrGETURL;
                wrGETURL = WebRequest.Create(URL);
                HttpWebResponse response = wrGETURL.GetResponse() as HttpWebResponse;

                StreamReader responseStream = new StreamReader(response.GetResponseStream());

                var result = responseStream.ReadToEnd();
                //var parsedInformation = JsonConvert.DeserializeObject<RootObject>(result);
                foreach (var forwardData in JsonConvert.DeserializeObject<RootObject>(result).forwards)
                {
                    //test output
                    Console.WriteLine(forwardData.id + " " + " " + forwardData.position + " " + forwardData.name + " " + forwardData.twitterURL + " " + forwardData.height + " " + forwardData.birthdate);

                    //write to database using EF
                    using (var _temp_Player = new DataFetcherDBEntities())
                    {
                        //var players = _temp_Player.Set<Player>();
                        _temp_Player.Players.Attach(new Player
                        {
                            player_id = forwardData.id,
                            name = forwardData.name,
                            age = forwardData.age,
                            birthdate = forwardData.birthdate,
                            birthplace = forwardData.birthplace,
                            weight = forwardData.weight,
                            height = forwardData.height,
                            number = forwardData.number,
                            position = forwardData.position,
                            imageUrl = forwardData.imageUrl,
                            twitterURL = forwardData.twitterURL,
                            twitterHandle = forwardData.twitterHandle
                        });
                        _temp_Player.SaveChanges();
                    }

                }

            }
            Console.ReadKey();

        }
    }
}

Any tips/ideas?
PS I'm still learning and self-taught.

*Edit, I copied the localdb to "...DataFetcher\\bin\\Debug\\Databases" and my new error is ""Violation of PRIMARY KEY constraint 'PK__tmp_ms_x__44DA120C6655986D'. Cannot insert duplicate key in object 'dbo.Player'. The duplicate key value is (8473492).\\r\\nThe statement has been terminated."}" Which doesn't make sense to me as every player has a unique ID (player_id = forwardData.id)

You need to use _temp_Player.Players.Add instead of Attach.

When you Attach, you need to set the entity state for the EF to detect that it is a new record and insert it into the database during Save Changes.

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