本文来自网络著作权归原作者http://blog.csdn.net/zezhi821/article/details/7235134

using System;
using System.Linq;

namespace EF
{
    class Program
    {
        static void Main(string[] args)
        {
            using (EF.DFEntities DF = new DFEntities())
            {
                #region 清空演练
                DF.ExecuteStoreCommand("delete from " + DF.UserInfoes.EntitySet.ElementType.Name);
                //因为表名与关键字重名,因此加中括号
                DF.ExecuteStoreCommand("delete from [" + DF.Users.EntitySet.ElementType.Name + "]");

                //表里头是否有数据
                bool HasData = DF.Users.Any();
                if (!HasData)
                {
                     Console.WriteLine("1、数据已经清空");
                     Console.WriteLine("————————————————");
                }
                #endregion

                #region 添加演练
                //创建实体类
                User setUser = new User();
                //赋值
                setUser.UserName = "Joe Cai";
                setUser.UserPassword = "123456";
                DF.AddToUsers(setUser);//添加父表信息

                UserInfo setUserInfo = new UserInfo();
                setUserInfo.UserEmail = "zezhi821@163.com";
                setUserInfo.UserMobile = "15111111111";
                setUserInfo.UserTelephone = "0755-12345678";
                setUser.UserInfoes.Add(setUserInfo);//添加子表信息

                //表里头是否有数据
                bool HasAddUser = DF.Users.Any();
                if (!HasAddUser)
                {
                    Console.WriteLine("2、未添加账号");
                    Console.WriteLine("————————————————");
                }

                DF.SaveChanges();//保存到数据库

                //表里头是否有数据
                HasAddUser = DF.Users.Any();
                if (HasAddUser)
                {
                    Console.WriteLine("3、已经添加账号");
                    Console.WriteLine("————————————————");
                }

                #endregion

                #region 读取演练

                UserInfo getUserInfo = DF.UserInfoes.Where(it => it.UserEmail == "zezhi821@163.com").FirstOrDefault();

                if (getUserInfo != null)
                {
                    if (getUserInfo.User == null)
                    {
                        getUserInfo.UserReference.Load();//加载父表信息
                    }

                    User getUser = getUserInfo.User;//因为已经加载,故能读取到父表信息
                    Console.WriteLine("————————————————");
                    Console.WriteLine("我的账号");
                    Console.WriteLine(string.Format("      UserId : {0}", getUser.UserId));
                    Console.WriteLine(string.Format("    UserName : {0}", getUser.UserName));
                    Console.WriteLine(string.Format("UserPassword : {0}", getUser.UserPassword));
                    Console.WriteLine("================================");
                    Console.WriteLine("我的账号详细信息");
                    Console.WriteLine(string.Format("   UserInfoId : {0}", getUserInfo.UserInfoId));
                    Console.WriteLine(string.Format("    UserEmail : {0}", getUserInfo.UserEmail));
                    Console.WriteLine(string.Format("   UserMobile : {0}", getUserInfo.UserMobile));
                    Console.WriteLine(string.Format("UserTelephone : {0}", getUserInfo.UserTelephone));
                    Console.WriteLine("————————————————");
                }
                else
                {
                    Console.WriteLine("没有相关账号信息");
                }

                #endregion

                Console.ReadLine();
            }
       }
}