我是 asp.net mvc 的新手。
我正在使用 Linq to Sql 并尝试做所有松散耦合的事情。
我有两张桌子:
- 消息
- 新闻档案
我正在尝试做的是保存新闻并同时上传其文件。
如何将新闻与他的文件一起保存到 NewsFiles 表中?
Linq to Sql 模型是好的,它包括对象 NewsFile 到 News 对象。
我的新闻表的具体存储库类(葡萄牙语中的 noticia):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MagixCMS.Models
{
public class NoticiaRepository : INoticiaRepository
{
#region INoticiaRepository Members
magixcmsEntities _entities = new magixcmsEntities();
public noticia CreateNoticia(noticia noticiaToCreate)
{
_entities.AddTonoticiaSet(noticiaToCreate);
_entities.SaveChanges();
return noticiaToCreate;
}
public void DeletaNoticia(noticia noticiaToDelete)
{
var noticiaOriginal = GetNoticia(noticiaToDelete.Id);
_entities.DeleteObject(noticiaOriginal);
_entities.SaveChanges();
}
public noticia EditNoticia(noticia noticiaToEdit)
{
var noticiaOriginal = GetNoticia(noticiaToEdit.Id);
_entities.ApplyPropertyChanges(noticiaToEdit.EntityKey.EntitySetName, noticiaToEdit);
_entities.SaveChanges();
return noticiaToEdit;
}
public noticia GetNoticia(int id)
{
return (from c in _entities.noticiaSet where c.Id == id select c).FirstOrDefault();
}
public IEnumerable<noticia> ListNoticias()
{
return _entities.noticiaSet.ToList();
}
#endregion
}
}
看这不是对 NewsFile 对象的任何提及。